Categories
JavaScript Answers

How to match any character that is not a letter or number with a regular expression in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *