How to remove all Non-ASCII characters from the string using JavaScript ? Last Updated : 16 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 regExUsing Unicode in JavaScript regExUsing ASCII values with the Array filter methodApproach 1: Using ASCII values in JavaScript regExThis 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(); OutputHidd©©©en??Ascii ©©®®®Charac££ter Hidden??Ascii CharacterApproach 2: Using Unicode in JavaScript regExThis 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(); OutputHidd©©©en??Ascii ©©®®®Charac££ter Hidden??Ascii CharacterApproach 3: Using ASCII values with the Array filter methodThis 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(); OutputHidd©©©en??Ascii ©©®®®Charac££ter Hidden??Ascii Character Approach 4: Using ES6 methods with a custom functionThis approach utilizes the String.prototype.replace() method with a custom function that removes non-ASCII characters from the string. JavaScript // 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, ""); } // Function to remove non-ASCII characters // and display the output function gfg_Run() { // Using custom function with replace method str = removeNonASCII(str); // Display output console.log(str); } // Function call gfg_Run(); OutputHidd©©©en??Ascii ©©®®®Charac££ter Hidden??Ascii Character Comment More infoAdvertise with us Next Article How to remove all Non-ASCII characters from the string using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies ASCII javascript-string JavaScript-DSA JavaScript-Questions +2 More Similar Reads JavaScript - Strip All Non-Numeric Characters From String Here are the different methods to strip all non-numeric characters from the string.1. Using replace() Method (Most Common)The replace() method with a regular expression is the most popular and efficient way to strip all non-numeric characters from a string.JavaScriptconst s1 = "abc123xyz456"; const 2 min read JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is 3 min read How to remove HTML tags from a string using JavaScript ? Removing HTML tags from a string in JavaScript means stripping out the markup elements, leaving only the plain text content. This is typically done to sanitize user input or to extract readable text from HTML code, ensuring no unwanted tags remain in the string.HTML tags come in two forms: opening t 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read How to Remove Spaces From a String using JavaScript? These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.split() method is used to split a string into multiple sub-strings and return them in the form of an array. The join() method is used to join an array of strings using 2 min read JavaScript - How to Get Character Array from String? Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf 2 min read How to convert character to ASCII code using JavaScript ? The purpose of this article is to get the ASCII code of any character by using JavaScript charCodeAt() method. This method is used to return the number indicating the Unicode value of the character at the specified index. Syntax: string.charCodeAt(index); Example: Below code illustrates that they ca 1 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read JavaScript - Characterrs to Opposite Case in a String Here are the various approaches to convert each character of a string to its opposite case (uppercase to lowercase and vice versa).Using for Loop and if-else Condition - Most CommonIn this approach, we iterate through each character of the string. Using an if-else statement, it checks if each charac 3 min read How to trim a file extension from string using JavaScript ? Given a fileName in string format and the task is to trim the file extension from the string using JavaScript. replace() method: This method searches a string for a defined value or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newv 4 min read Like