Remove all non-alphanumeric Characters from a String in JS

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Remove all non-alphanumeric Characters from a String in JS

Use the String.replace() method to remove all non-alphanumeric characters from a string, e.g. str.replace(/[^a-z0-9]/gi, '');.

The replace() method will remove all non-alphanumeric characters from the string by replacing them with empty strings.

index.js
const str = 'A!@#b$%^c&*('; const replaced = str.replace(/[^a-z0-9]/gi, ''); console.log(replaced); // ๐Ÿ‘‰๏ธ Abc

remove all non alphanumeric characters from string

The code for this article is available on GitHub
If you also want to preserve spaces, hyphens or other characters, scroll down to the next code snippet.

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

The forward slashes / / mark the beginning and end of the regular expression.

index.js
const str = 'A!@#b$%^c&*('; const replaced = str.replace(/[^a-z0-9]/gi, ''); console.log(replaced); // ๐Ÿ‘‰๏ธ Abc

The square brackets [] are called a character class.

The caret ^ symbol means "NOT the following". In our case, this means not any letter in the range of a-z and not any number in the range of 0-9.

We used the g (global) flag because we want to match all occurrences of non-alphanumeric characters and not just the first occurrence.

The i flag makes our match case-insensitive by targeting all uppercase and lowercase characters.

If your use case requires you to also preserve spaces, hyphens or other characters, add them between the square brackets [].

index.js
const str = 'A!@# b$% ^c&-*('; const replaced = str.replace(/[^a-z0-9 -]/gi, ''); console.log(replaced); // ๐Ÿ‘‰๏ธ A b c-

The code sample preserves all alphanumeric characters, spaces and hyphens. You could adjust the regex to your needs by adding or removing characters between the square brackets [].

# Creating a reusable function

If you have to remove the non-alphanumeric characters from a string often, define a reusable function.

index.js
function removeNonAlphanumeric(string) { return string.replace(/[^a-z0-9]/gi, ''); } console.log(removeNonAlphanumeric('A(@#(@B$C')); // ๐Ÿ‘‰๏ธ ABC console.log(removeNonAlphanumeric('A_@#B(***C((_D')); // ๐Ÿ‘‰๏ธ ABCD

creating reusable function

The code for this article is available on GitHub

The removeNonAlphanumeric() function takes a string as a parameter and removes all non-alphanumeric characters from the string.

# Remove all non-alphanumeric Characters from a String using \W

You can also use the \W special character to shorten your regex and remove all non-alphanumeric characters from a string.

index.js
function removeNonAlphanumeric(string) { return string.replace(/\W/g, ''); } console.log(removeNonAlphanumeric('A(@#(@B$C')); // ๐Ÿ‘‰๏ธ ABC console.log(removeNonAlphanumeric('A_@#B(***C((_D')); // ๐Ÿ‘‰๏ธ ABCD

remove all non alphanumeric characters from string using w

The \W special character is equivalent to [^A-Za-z0-9_].

In other words, the \W character matches:

  • any character that is not a word character from the basic Latin alphabet
  • non-digit characters
  • not underscores

Note that the \W special character doesn't remove the underscores from the string.

If you also need to remove the underscores, use the code sample from the previous subheading.

index.js
function removeNonAlphanumeric(string) { return string.replace(/[^a-z0-9]/gi, ''); } console.log(removeNonAlphanumeric('A(@#(@B$C')); // ๐Ÿ‘‰๏ธ ABC console.log(removeNonAlphanumeric('A_@#B(***C((_D')); // ๐Ÿ‘‰๏ธ ABCD
The code for this article is available on GitHub

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev