Categories
JavaScript Answers

How to remove non-ASCII character in string with JavaScript?

Spread the love

Sometimes, we want to remove non-ASCII character in string with JavaScript.

In this article, we’ll look at how to remove non-ASCII character in string with JavaScript.

How to remove non-ASCII character in string with JavaScript?

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.

For instance, we write

const newStr = str.replace(/[^\x00-\x7F]/g, "");

to call str.replace with the /[^\x00-\x7F]/g regex to replace all characters that don’t have character code between 0 and 127 with empty strings.

We use the g flag to find all matches of the regex pattern in str.

Conclusion

To remove non-ASCII character in string with JavaScript, we use the string’s replace method.

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 *