Removing non-alphanumeric chars

The following is the/a correct regex to strip non-alphanumeric chars from an input string:

input.replace(/\W/g, '')

Note that \W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.:

input.replace(/[^0-9a-z]/gi, '')

The input is malformed

Since the test string contains various escaped chars, which are not alphanumeric, it will remove them.

A backslash in the string needs escaping if it's to be taken literally:

"\\test\\red\\bob\\fred\\new".replace(/\W/g, '')
"testredbobfrednew" // output

Handling malformed strings

If you're not able to escape the input string correctly (why not?), or it's coming from some kind of untrusted/misconfigured source - you can do something like this:

JSON.stringify("\\test\red\bob\fred\new").replace(/\W/g, '')
"testredbobfrednew" // output

Note that the json representation of a string includes the quotes:

JSON.stringify("\\test\red\bob\fred\new")
""\\test\red\bob\fred\new""

But they are also removed by the replacement regex.

Answer from AD7six on Stack Overflow
Discussions

regex - Remove all non alphanumeric and any white spaces in string using javascript - Stack Overflow
I'm trying to remove any non alphanumeric characters ANY white spaces from a string. Currently I have a two step solution and would like to make it in to one. var name_parsed = name.replace(/[^0-... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Regex to remove all non alpha-numeric and replace spaces with + - Stack Overflow
I'm looking to use regex to try remove all non alpha-numeric characters from a string and replace spaces with a + All I want to permit is basically alphabetical words A-Z and + This is specifical... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 20, 2015
regex - Remove all characters except alphanumeric and spaces with javascript - Stack Overflow
I like the solution povided by "Remove not alphanumeric characters from string. Having trouble with the [\] character" but how would I do this while leaving the spaces in place? I need to tokenize More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Removing non-alphanumeric text with String.prototype.replace - Stack Overflow
I'm trying to strip a string of all characters that are not a letter or a number. I tried String.prototype.replace with a regular expression, but it didn't remove the expected characters: this. More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 8, 2018
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ removing all non-letter characters from a string? ((using regex))
r/learnjavascript on Reddit: removing all non-letter characters from a string? ((using regex))
September 28, 2015 -

I am currently trying this two different ways and they aren't working - I'm checking the input value and running this every time a key is pressed:

input.replace(/[^a-zA-z]/, "");

and

for(var i = 0; i < input.length; i++){
	   if( input[i] ==  /[^a-zA-z]/g){
	   	console.log("input " + input[i] + " is not a letter!");
	    	input.replace(input[i], "");
	   }
	}

jsfiddle here

๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-non-alphanumeric-characters-from-string
Remove all non-alphanumeric Characters from a String in JS | bobbyhadz
The removeNonAlphanumeric() function takes a string as a parameter and removes all non-alphanumeric characters from the string. You can also use the \W special character to shorten your regex and remove all non-alphanumeric characters from a string.
๐ŸŒ
Melvin George
melvingeorge.me โ€บ blog โ€บ remove-all-non-alphanumeric-characters-string-javascript
How to remove all the non-alphanumeric characters from a string using JavaScript? | MELVIN GEORGE
June 23, 2021 - ... // a string const str = "#HelloWorld123$%"; // regex expression to match all // non-alphanumeric characters in string const regex = /[^A-Za-z0-9]/g; // use replace() method to // match and remove all the // non-alphanumeric characters const ...
๐ŸŒ
Linux Genie
linuxgenie.net โ€บ home โ€บ javascript remove non-alphanumeric characters
JavaScript Remove non-Alphanumeric Characters - Linux Genie
June 16, 2023 - Example In the given example, first, ... non-alphanumeric characters, we will use the regex pattern in the โ€œreplace()โ€ method that will replace these characters with the empty string: Finally, print the resultant string on the ...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ remove non alphanumeric characters using javascript
How to Remove Non Alphanumeric Characters Using JavaScript | Delft Stack
February 2, 2024 - This method is very useful if we want to replace non-alphanumeric characters in all elements of an array. Here, we discard everything except English alphabets (Capital and small) and numbers. The g modifier says global, and i matches case-insensitivity. var input = '123abcABC-_*(!@#$%^&*()_-={}[]:\"<>,.?/~`'; var stripped_string = input.replace(/[^a-z0-9]/gi, ''); console.log(stripped_string); ... We can optimize the regex used in the above code by replacing it with /[\W]/g where \W is equivalent to [^0-9a-zA-Z]. So, our final regex would be /[\W_]/g because we remove underscore from the input string.
Find elsewhere
๐ŸŒ
Itsourcecode
itsourcecode.com โ€บ home โ€บ how to remove all non-alphanumeric characters in javascript?
How to remove all non-alphanumeric characters in JavaScript? - Itsourcecode.com
June 27, 2023 - We can use the regex Metacharacter (\W) to remove all special characters, including spaces, from a string. ... To make your regular expression shorter and get rid of any non-alphanumeric characters in a string, you can use the special character ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ removing-all-non-alphabetic-characters-from-a-string-in-javascript
Removing all non-alphabetic characters from a string in JavaScript
March 18, 2025 - In the following example, we have defined a function called remove Nonalphabetic that uses regular expressions with the replace method. This function takes the string, replaces all non-alphabetic characters from it, and returns it.
๐ŸŒ
Code Highlights
code-hl.com โ€บ home โ€บ javascript โ€บ tutorials
How to Remove Non Alphanumeric Characters JavaScript for Cleaner Code | Code Highlights
September 30, 2024 - In this tutorial, we will explore how to remove non-alphanumeric characters in JavaScript, making your code cleaner and more efficient. ... To remove non-alphanumeric characters, we often use the String.prototype.replace() method. The syntax looks like this: 1string.replace(regexp|substr, newSubStr|function)
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ javascript โ€บ examples โ€บ faq โ€บ javascript-how-to-remove-non-alphanumeric-characters-from-string
How to Remove All Non-Alphanumeric Characters from a String in JavaScript | Tutorial Reference
Solution: this regular expression finds any character that is not a letter or a number and replaces it with an empty string. ... The regex /[^a-z0-9]/gi is the key to this operation.
๐ŸŒ
Regex101
regex101.com โ€บ r โ€บ jI5hK6 โ€บ 1
regex101: Remove Non-Alphanumeric Characters
Online regex tester and debugger. Test, explain, benchmark, and generate code for PCRE2, JavaScript, Python, Go, Java, .NET, and Rust.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ javascript regex alphanumeric
Regular Expression for JavaScript to Allow Only Alphanumeric Characters | Delft Stack
October 12, 2023 - To remove non-alphanumeric characters from a string, you will first call the replace() method and pass a regular expression (RegEx) that matches all non-alphanumeric characters as the first parameter and an empty string as the second parameter.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ js_regex_remove_non_alphanumeric.php
js regex remove non alphanumeric: Master Clean Strings in JavaScript โ€“ The Ultimate Guide to Perfect Data!
A: This regex pattern means "match any character that is not (^) an uppercase letter (A-Z), a lowercase letter (a-z), or a digit (0-9), and do this for all occurrences (g) in the string." The matched non-alphanumeric characters are then replaced, typically with an empty string, effectively ...