The test() function is one of the methods that can be used to check if a string contains special characters in JavaScript. In addition you can find some other methods introduced in this article. Please read the article below and draw your ways to do it.
What are special characters?
Special characters are characters that are not written in the standard form. From the outside, it has strange shapes. They do not need to adhere to a specific rule when paired together. Each combination carries a unique meaning and is according to the purpose of its creator.
Therefore, it is difficult for an outsider to explain what the particular character combination means clearly. Usually, when looking at those lines of characters, one can only see them as beautiful.
Regex
The term “regular expression,” also known as its expression, is utilized for advanced string processing. These expressions will have their own rules, which you must adhere to for your expression to be novel. The String can also shorten to RegEx and the Regular name Expression.
Some methods in javascript
To determine whether a pattern is present in a string, use the test() or search() methods; the exec() or match() methods provide additional information at a slower execution rate.
Code:
var regexIT = /LearnShareIT/; var string = "Welcome to LearnShareIT"; console.log(regexIT.test(string));
Output:
true
So we can see that the regexIT we tested is present in the given string, so it returns true in the console screen.
Check if string contains special characters in JavaScript
Using test() method
In this method, we will use the test() method to check if the String contains special characters or not. We first write a regex of special characters and then check the String. See the example below to see how to solve it.
Syntax:
RegExpObject.test(string)
Parameter:
- string: required.
Code:
var regexIT = /[`[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/; var string = "Welcome to LearnShareIT!"; console.log(regexIT.test(string));
Output:
true
In the above example, we see a unique character in the string, and the character is “!” so the program will return true to the console screen.
Using match() method
The Match() method will search for substrings that match the provided regular expression. The method will return the found strings as an array.
Syntax:
match(regexp)
Parameter:
- Regexp: an object with a regular expression or any object with the Symbol.
Code:
var regexIT = /[`[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/; var string = "Welcome to LearnShareIT!"; console.log(string.match(regexIT));
Output:
['!', index: 23, input: 'Welcome to LearnShareIT!', groups: undefined]
When you use the match() method in the above example, it will find the substring that matches the regex we have given and here the string “!”. The result returned on the console screen will be that substring value along with the starting position of the String and, finally, the input value.
Summary
To summarize, this article shows you two ways to check if a string contains special characters in JavaScript using the test() method or match() method to get the same result. If you have any problems, please comment below. We will respond as possible. Thanks for reading!
Maybe you are interested:
- Get the Last 2 Characters of a String in JavaScript
- Insert String at specific Index of another String in JS
- Check if String contains only Digits in JavaScript

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs