JavaScript: Check if String Starts with Special Character

A general requirement many people encounter while working with javascript strings is to check if the first letter of the string is a special character. This article demonstrates easy ways to check if the string starts with a special character using different methods and illustration examples.

Table of Contents:

Check If String Starts with Special Character using RegEXP and test()

JavasScript’s test() method will execute a search for a match between the regular expression and the specified string. This method takes an argument that is a string against which the regular expression is matched. If the match is found, true is returned. Else, false is returned.

Syntax: test(string)

Example:-

Check if the below strings start with a special character

  • “#1234javaScript”
  • “&JAVASCRIPT”
  • “JAVASCRIPT@”

Code:-

function checkFirstLetterSpecial(_string)
{
let spCharsRegExp = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
return spCharsRegExp.test( _string);
}
//examples
let dummyString1 = "#1234javaScript";
let dummyString2 = "&JAVASCRIPT";
let dummyString3 = "JAVASCRIPT@";
//usage of the function 
console.log(checkFirstLetterSpecial(dummyString1));
console.log(checkFirstLetterSpecial(dummyString2));
console.log(checkFirstLetterSpecial(dummyString3));

Output:-

true
true
false

Explanation:-

  • Here in the above code test() method is used to match the pattern against the _string passed as an argument, the regular expression is stored in spCharsRegExp variable which contains all the special characters including:
  • / and / specifying the start and end of the expression.
  • ^ specifying the beginning of the string
  • !@#$%^&*()_+-=[]{};’:”\|,.<>\/? are the special characters
  • If the expression is matched, then return true; otherwise, false is returned.

Check If String Starts with Special Character using RegEXP and match()

JavaScript’s match() method will return the result of a string matching against a regular expression. This method takes a regular expression object as a parameter and returns an array of matching results. 

Syntax: match(regExp)

Example:-

Check if the below strings start with a special character

  • “#1234javaScript”
  • “&JAVASCRIPT”
  • “JAVASCRIPT@”

Code:-

function checkFirstLetterSpecial(_string)
 {
     let spCharsRegExp = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
     return _string.match(new RegExp(spCharsRegExp)) !== null;    
 }
//examples
let dummyString1 = "#1234javaScript";
let dummyString2 = "&JAVASCRIPT";
let dummyString3 = "JAVASCRIPT@";
//usage of the function 
console.log(checkFirstLetterSpecial(dummyString1));
console.log(checkFirstLetterSpecial(dummyString2));
console.log(checkFirstLetterSpecial(dummyString3));

Output:-

true
true
false

Explanation:-

  • The method match() is called here in the above code by the calling string. The regular expression  /^[!@#$%^&*()_+-=[]{};’:”\|,.<>\/?]+/ is passed as an argument to the match() method  . 
  • / and / specify the start and end of expression.
  • ^ specifies the beginning character.
  • !@#$%^&*()_+-=[]{};’:”\|,.<>\/? are the special characters.
  • If the first character is a special character from the pattern, the matching expression is returned. Else, null is returned. 
  • Hence, if something is retrieved, the first character is a special character. Otherwise, it is not.

Check If String Starts with a Special Character using ASCII Code

ASCII codes are the numeric value given to the characters and symbols for storing and manipulation by computers.

JavaScript’s charCodeAt() method will return an integer ranging from 0 and 65535 that depicts the UTF-16 code value at the given index.

Example:-

Check if the below strings start with a special character

  • “#1234javaScript”
  • “&JAVASCRIPT”
  • “JAVASCRIPT@”

Code:-

function checkFirstLetterSpecial(_string) 
{
    if ((_string.charCodeAt(0) >= 32 && _string.charCodeAt(0) <= 47)
        || (_string.charCodeAt(0) >= 58 && _string.charCodeAt(0) <= 64)
        || (_string.charCodeAt(0) >= 91 && _string.charCodeAt(0) <= 96)
        || (_string.charCodeAt(0) >= 123 && _string.charCodeAt(0) <= 126))
        {
            return true;
        }
        else return false;
}
//examples
let dummyString1 = "#1234javaScript";
let dummyString2 = "&JAVASCRIPT";
let dummyString3 = "JAVASCRIPT@";
//usage of the function 
console.log(checkFirstLetterSpecial(dummyString1));
console.log(checkFirstLetterSpecial(dummyString2));
console.log(checkFirstLetterSpecial(dummyString3));

Output:-

true
true
false

Explanation:-

  • Here in the above code, we check if the character’s ASCII code placed at 0th index falls between the below ranges :
  • 32 and 47(both included). -> <space> 32, ! 33, ” 34, # 35, $ 36, % 37, & 38, ‘ 39, ( 40, ) 41, * 42, + 43, , 44, – 45, . 46, / 47
  • 58 and 64(both included). -> : 58, ; 59, < 60, = 61, > 62, ? 63, @ 64
  • 91 and 96(both included) -> [ 91, \ 92, ] 93, ^ 94, _ 95, ` 96
  • 123 and 126(both included). -> { 123 | 124 } 125 ~ 126
  • This check is made using the method charCodeAt(0).

I hope this article helped you check if the first letter is a special character in a javascript string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top