Sometimes, we want to match special characters and letters in regex in JavaScript.
In this article, we’ll look at how to match special characters and letters in regex in JavaScript.
How to match special characters and letters in regex in JavaScript?
To match special characters and letters in regex in JavaScript, we create a regex that matches the special characters we want.
For instance, we write
const pattern = /^[\w&.-]+$/;
if (pattern.test(qry)) {
// ...
}
to define the pattern
regex.
In it, we put the special characters we want to match in the square brackets.
Then we call pattern.test
to check if the qry
string matches the pattern.
Conclusion
To match special characters and letters in regex in JavaScript, we create a regex that matches the special characters we want.