JavaScript provides several ways to remove non-ASCII characters from a string. Regular expressions can directly match characters outside the ASCII range, while array methods can filter characters based on their character codes.
- ASCII characters have character codes from 0 to 127.
- Use replace() with a regular expression to remove non-ASCII characters directly.
- Use filter() and charCodeAt() when you want to process each character individually.
Approach 1: Using ASCII Values in Regular Expression
A regular expression can match every character outside the ASCII range using [^\x00-\x7F]. The replace() method then replaces these characters with an empty string.
Example: In this example, we remove all characters whose ASCII values are greater than 127.
// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
// Remove non-ASCII characters
function gfg_Run() {
str = str.replace(/[^\x00-\x7F]/g, "");
// Display output
console.log(str);
}
// Function call
gfg_Run();
Approach 2: Using Unicode Range in Regular Expression
Unicode escape sequences can also be used to identify characters outside the ASCII range. The range \u{0080}-\u{FFFF} represents characters starting from code point 128.
Example: In this example, we use a Unicode regular expression to remove non-ASCII characters.
// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
// Remove non-ASCII characters
function gfg_Run() {
str = str.replace(/[\u{0080}-\u{FFFF}]/gu, "");
// Display output
console.log(str);
}
// Function call
gfg_Run();
- \u{0080}-\u{FFFF} represents the specified Unicode range above ASCII.
- The u flag enables Unicode-aware matching.
- The g flag removes all matching characters.
Note: This pattern does not cover every Unicode code point above U+FFFF. For a general solution that removes everything outside ASCII, the [^\x00-\x7F] pattern is preferable.
Approach 3: Using ASCII Values with filter()
The filter() method can be combined with split() and charCodeAt() to keep only characters whose ASCII codes are between 0 and 127.
Example: In this example, we split the string into individual characters and filter out characters with ASCII values greater than 127.
// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
// Remove non-ASCII characters
function gfg_Run() {
str = str
.split("")
.filter(function (char) {
return char.charCodeAt(0) <= 127;
})
.join("");
// Display output
console.log(str);
}
// Function call
gfg_Run();
Approach 4: Using replace() with a Custom Function
A reusable function can be created around the replace() method. This makes the operation easier to use with multiple strings.
Example: In this example, we create a custom removeNonASCII() function to remove all non-ASCII characters.
// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
// Function to remove non-ASCII characters
function removeNonASCII(input) {
return input.replace(/[^\x00-\x7F]/g, "");
}
// Remove non-ASCII characters
function gfg_Run() {
str = removeNonASCII(str);
// Display output
console.log(str);
}
// Function call
gfg_Run();