Javascript: Check if string contains special characters

This article will discuss checking if a string contains special characters in a javascript string using different methods and example illustrations.

Table of Contents:

  1. Check if a string contains special characters using test()
  2. Check if a string contains special characters using indexOf()

Check if a string contains special characters using test()

Javascript’s test(string) method searches for a match within the string and returns true if the match is found. Else returns false.

This method takes an argument that is a string against which the regular expression is matched.

Example1:

Check if the below strings contain special characters

  • “java%@#&*90$~
  • “javascript”

Function Code:-

function checkIfStringHasSpecialChar(_string)
{
    let spChars = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
    if(spChars.test(_string)){
      return true;
    } else {
      return false;
    }
}

Usage:-

let string1= "java%@#&*90$~"
let string2 = "javascript"
console.log(checkIfStringHasSpecialChar(string1));
console.log(checkIfStringHasSpecialChar(string2));

Output:-

true
false

Explanation:-

  • Here in the above code test() function is used. The Regular expression is /[!@#$%^&*()_+-=[]{};’:”\|,.<>\/?]+/
  • If any of the characters from the regular expression is found within the string, true is returned. Else false is returned.
  • Since string1 has special characters, true is returned.
  • Since string2 do not have any special characters, false is returned.

Check if a string contains special characters using indexOf()

Javascript’s indexOf(searchValue, fromIndex) method returns the index of the first occurrence of the specified searchValue within the calling string starting from the fromIndex argument. This method returns -1 if the searchValue is not found.

The second argument fromIndex is optional, and the default value is 0.

Example1:

Check if the below strings contain special characters

  • “java%@#&*90$~
  • “javascript”

Function Code:-

function checkIfStringHasSpecialChar(_string) {
    let spChar = "/[!@#$%^&*()_+\-=\[\]{};':\\|,.<>\/?]+/";

    for (var i = 0; i < _string.length; i++) {
       if (spChar.indexOf(_string.charAt(i)) != -1) {
           return true;
       }
    }
    return false;
}

Usage:-

let string1= "java%@#&*90$~"
let string2 = "javascript"
console.log(checkIfStringHasSpecialChar(string1));
console.log(checkIfStringHasSpecialChar(string2));

Output:-

true
false

Explanation:-

  • Here in the above code indexOf() function is used.
  • Each character of the string passed (_string) is matched with the calling string (“/[!@#$%^&*()_+-=[]{};’:\|,.<>\/?]+/”). If the searchValue is found within the calling string, true is returned. Else, false is returned.
  • Since string1 has special characters true is returned.
  • Since string2 do not have any special characters false is returned.

Note that the string spChar does not have the double quotes.

Read More:

I hope this article helped you to check if a javascript string contains special characters. 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