How to useUnicode in JavaScript regEx in Javascript
- This approach uses a Regular Expression to remove the Non-ASCII characters from the string like in the previous example.
- It specifies the Unicode for the characters to remove. The range of characters between (0080 – FFFF) is removed.
- Use .replace() method to replace the Non-ASCII characters with the empty string.
Example: This example implements the above approach.
// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
// Function to remove ASCII characters
// and display the output
function gfg_Run() {
// Using RegEx and replace method with Unicodes
str = str.replace(/[\u{0080}-\u{FFFF}]/gu, "");
// Display output
console.log(str);
}
// Funcion call
gfg_Run();
Output
Hidd©©©en??Ascii ©©®®®Charac££ter Hidden??Ascii Character
How to remove all Non-ASCII characters from the string using JavaScript ?
In this article, we are given a string containing some non-ASCII characters and the task is to remove all non-ASCII characters from the given string.
Approaches to remove all Non-ASCII Characters from String:
Table of Content
- Using ASCII values in JavaScript regEx
- Using Unicode in JavaScript regEx
- Using ASCII values with the Array filter method