Sometimes, we want to match any character that is not a letter or number with a regular expression in JavaScript.
In this article, we’ll look at how to match any character that is not a letter or number with a regular expression in JavaScript.
How to match any character that is not a letter or number with a regular expression in JavaScript?
To match any character that is not a letter or number with a regular expression in JavaScript, we can use the [^a-zA-Z0-9]
regex pattern.
For instance, we write
let str = "dfj,dsf7lfsd .sdklfj";
str = str.replace(/[^A-Za-z0-9]/g, " ");
to call str.replace
with the /[^A-Za-z0-9]/g
regex to replace all non alphanumeric characters with spaces.
Conclusion
To match any character that is not a letter or number with a regular expression in JavaScript, we can use the [^a-zA-Z0-9]
regex pattern.