To remove all special characters except space from a string in JavaScript, call the replace()
method on the string with the regular expression /[^a-zA-Z0-9\s]/g
as a first parameter and an empty string(''
) as a second parameter. This will replace all occurrences of the special characters(except space) with an empty string(''
).
The String.replace()
method takes a pattern(string or regular expression) as a first parameter, searches for this pattern in the given string and replaces the matches with the string passed as the second parameter.
The regular expression /[^a-zA-Z0-9\s]/g
matches any character that is not a letter, digit, or whitespace character.
The following example removes all special characters except space from the given string:
// String with special characters const str = "Hello!@# World&*("; // Remove all special characters except space const newStr = str.replace(/[^a-zA-Z0-9\s]/g, ''); // Print the new string console.log(newStr);
Output:
"Hello World"
In the regular expression /[^a-zA-Z0-9\s]/g
:
- The forward slashes (
/
) at the beginning and end of the regular expression indicate the start and end of the pattern. - The
^
character inside square brackets ([]
) indicates negation or exclusion, which means that the regular expression matches any character that is not in the set of characters specified. - The character set
[a-zA-Z0-9\s]
includes all uppercase and lowercase letters, digits, and whitespace characters. - The
\s
character represents any whitespace character, including spaces, tabs, and line breaks. - The
/g
flag at the end of the regular expression stands for “global,” which means that the regular expression will match all occurrences in the input string, rather than stopping after the first match.
If you want to include any additional character, you can add it to the character set([]
) of the regular expression.
Use ASCII Codes to Remove Special Characters from the String
In this approach, we loop through each character in the string and check its ASCII value using the charCodeAt()
method.
If the ASCII code falls in the below range, we include the character to the result string, otherwise exclude it:
- 65 – 90(A to Z)
- 97 – 122(a to z)
- 48 – 57(digits 0 to 9)
- 32(Space character)
For example:
// String with special characters const str = "Hello!@# World&*("; // Create an empty string let result = ""; // Loop through original string for (let i = 0; i < str.length; i++) { // Get the ASCII code of current character let charCode = str.charCodeAt(i); // Check if character isn't a special character if ( (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || (charCode>=48 && charCode <= 57) || charCode === 32) { result += str.charAt(i); } } // Print the result console.log(result);
Output:
"Hello World"
You can use either of the two methods to remove the special characters from a given string.
The first method is pretty straightforward. It gives you the desired result in just a single line of code.
The second method on the other hand needs a bit more efforts. You have to write more code and get less work done. However, this gives you clarity on how the things are actually working behind the scenes.
That’s all for the day. Happy Coding.