How to Remove Non-Alphanumeric Characters from a String in JavaScript?

Estimated read time 1 min read

To remove non-alphanumeric characters from a string in JavaScript, you can use the replace() method and a regular expression. Here’s an example:

let str = "This is a string!";
let newStr = str.replace(/[^a-zA-Z0-9]/g, "");

This code removes all non-alphanumeric characters from the string "This is a string!" using the replace() method and a regular expression and logs the modified string: "Thisisastring".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply