Friday, September 5, 2025
HomeLanguagesJavascriptHow to remove all Non-ASCII characters from the string using JavaScript ?

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. There are two methods to solve this problem which are discussed below: 

Approaches to remove all Nom-ASCII Characters from String:

Approach 1: Using ASCII values in JavaScript regEx

  • This approach uses a Regular Expression to remove the Non-ASCII characters from the string.
  • Only characters that have values from zero to 127 are valid. (0x7F is 127 in hex).
  • Use the .replace() method to replace the Non-ASCII characters with the empty string.

Example: This example implements the above approach. 

Javascript




// 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 Ascii values
    str = str.replace(/[^\x00-\x7F]/g, "");
     
    // Display output
    console.log(str);
}
 
// Funcion call
gfg_Run();


Output

Hidd©©©en??Ascii ©©®®®Charac££ter
Hidden??Ascii Character

Approach 2: Using Unicode in JavaScript regEx

  • 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. 

Javascript




// 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

Approach 3: Using ASCII values with the Array filter method

This approach uses the Array filter along with the JavaScript split method to filter out the ASCII-valid characters from the input string.

Example: This example demonstrates the above approach.

Javascript




// Input String
let str = "Hidd©©©en??Ascii ©©®®®Charac££ter";
// Display input string
console.log(str);
 
// Funciot to remove ASCII characters
// and display the output
function gfg_Run() {
     
    // Using array.filter with ASCII values
    str = str
    .split("")
    .filter(function (char) {
      return char.charCodeAt(0) <= 127;
    })
    .join("");
     
    // Display output
    console.log(str);
}
 
// Funcion call
gfg_Run();


Output

Hidd©©©en??Ascii ©©®®®Charac££ter
Hidden??Ascii Character

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS