Check if String contains Special Characters in JavaScript

avatar
Borislav Hadzhiev

Last updated: Jan 1, 2023
3 min

banner

# Check if String contains Special Characters in JavaScript

To check if a string contains special characters, call the test() method on a regular expression that matches any special character.

The test() method will return true if the string contains at least 1 special character and false otherwise.

index.js
function containsSpecialChars(str) { const specialChars = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/; return specialChars.test(str); } console.log(containsSpecialChars('hello!')); // ๐Ÿ‘‰๏ธ true console.log(containsSpecialChars('abc')); // ๐Ÿ‘‰๏ธ false console.log(containsSpecialChars('one two')); // ๐Ÿ‘‰๏ธ false if (containsSpecialChars('hello!')) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string contains special characters'); } else { console.log('โ›”๏ธ string does NOT contain special characters'); }

check if string contains special characters

If you consider spaces to be special characters, add a space between the square brackets [].
index.js
// ๐Ÿ‘‡๏ธ with space as a special character // ๐Ÿ‘‡๏ธ const specialChars = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;

We used the RegExp.test method to check if a string contains special characters.

The method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the beginning and end of the regular expression.

The square brackets [] are called a character class and match any of the characters between the brackets.

If you don't consider any of the characters between the brackets a special character, remove them.

Alternatively, you can use the Array.some() method.

# Check if String contains Special Characters using Array.some()

This is a four-step process:

  1. Define a string containing all special characters.
  2. Use the String.spit() method to split the string on each character.
  3. Use the Array.some() method to iterate over the array of special characters.
  4. Check if each special character is contained in the supplied string.
index.js
function containsSpecialChars(str) { const specialChars = `\`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~`; const result = specialChars.split('').some(specialChar => { if (str.includes(specialChar)) { return true; } return false; }); return result; } console.log(containsSpecialChars('hello!')); // ๐Ÿ‘‰๏ธ true console.log(containsSpecialChars('abc')); // ๐Ÿ‘‰๏ธ false console.log(containsSpecialChars('one two')); // ๐Ÿ‘‰๏ธ false if (containsSpecialChars('hello!')) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string contains special characters'); } else { console.log('โ›”๏ธ string does NOT contain special characters'); }

check if string contains special characters using array some

We defined a string containing all special characters. You can adjust the string depending on your needs.

We then used the String.split() method to split the string into an array of special characters.

index.js
// [ // '`', '!', '@', '#', '$', '%', // '^', '&', '*', '(', ')', '_', // '+', '-', '=', '[', ']', '{', // '}', ';', "'", ':', '"', '\\', // '|', ',', '.', '<', '>', '/', // '?', '~' // ] console.log(specialChars.split(''));

We used the Array.some() method to iterate over the array of special characters.

The function we passed to the some() method gets called with each element (special character) until it returns a truthy value or iterates over the entire array.

On each iteration, we check if the current special character is contained in the supplied string.

If the string contains at least 1 special character, the some() method returns true and short-circuits.

If the string doesn't contain any special characters, the some() method iterates over the entire array and returns false.

My personal preference is to use a regular expression in this scenario. Both approaches are a bit difficult to read, however, the regex option is a little more direct.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev