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
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-remove-non-alphanumeric-characters-from-a-string
JavaScript Program to Remove Non-Alphanumeric Characters from a String - GeeksforGeeks
July 23, 2025 - In this approach, we convert the input string to an array of characters using Array.from(), then use the filter method to keep only alphanumeric characters. Finally, we convert the filtered array back into a string using join(). Example: This example demonstrates how to remove non-alphanumeric characters using the filter method and string conversion.
Discussions

Check for Palindromes: Would a ``` for loop ``` help? Removing non-Alphanumeric Characters in Javascript?
Not sure how to get started on this one. I've heard a for loop is a good idea. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
19
1
January 11, 2017
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 - Removing non-alphanumeric text with String.prototype.replace - Stack Overflow
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Stack Overflow chat opening up to all users in January; Stack Exchange chat... Modernizing curation: A proposal for The Workshop and The Archive ... 1 Reduce multiple occurences of any non-alphanumeric characters in string down to one, **NOT REMOVE... More on stackoverflow.com
🌐 stackoverflow.com
October 8, 2018
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
🌐
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

🌐
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 - The JSON.stringify() converts an object to a string, and the resulting string follows the JSON notation. This method is very useful if we want to replace non-alphanumeric characters in all elements of an array.
🌐
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 - To remove all non-alphanumeric characters from a string in JavaScript, you can use the String.replace() method to get rid of any characters in a string that are not letters or numbers.
🌐
Linux Genie
linuxgenie.net › home › javascript remove non-alphanumeric characters
JavaScript Remove non-Alphanumeric Characters - Linux Genie
June 16, 2023 - It provides a powerful way to remove non-alphanumeric characters from a text or string. The replace() method searches for a specified pattern in a text and replaces it with a new value.
🌐
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 explored how to remove non-alphanumeric characters in JavaScript. We learned about the replace() method, its syntax, and how to use regular expressions to clean up strings effectively.
Find elsewhere
🌐
GitHub
gist.github.com › protzi › 39fc8c5c9b5ad58fa3b2
Remove all non-alphanumeric characters (punctuation, spaces and symbols) · GitHub
Remove all non-alphanumeric characters (punctuation, spaces and symbols) Raw · nonNumerical.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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 newStr = str.replace(regex, ""); console.log(newStr); // HelloWorld123
🌐
TutorialsPoint
tutorialspoint.com › article › removing-all-non-alphabetic-characters-from-a-string-in-javascript
Removing all non-alphabetic characters from a string in JavaScript
March 15, 2026 - Regular expressions are the most efficient way of removing non-alphabetic characters from a string with the replace() method. The replace() method searches for a specified pattern within a string and replaces it with a new substring.
🌐
Codegive
codegive.com › blog › js_replace_non_alphanumeric.php
JS Replace Non-Alphanumeric: Clean Your Strings (2026) & Master Data Sanitization for Robust Apps!
To remove non-alphanumeric characters in JavaScript, use the replace() method with a regular expression like /[^a-zA-Z0-9]/g. This pattern matches any character that is not a letter (a-z, A-Z) or a digit (0-9) globally.
🌐
GitHub
github.com › bobbyhadz › javascript-remove-non-alphanumeric-characters-from-string
GitHub - bobbyhadz/javascript-remove-non-alphanumeric-characters-from-string: A repository for an article at https://bobbyhadz.com/blog/javascript-remove-non-alphanumeric-characters-from-string · GitHub
A repository for an article at https://bobbyhadz.com/blog/javascript-remove-non-alphanumeric-characters-from-string - bobbyhadz/javascript-remove-non-alphanumeric-characters-from-string
Author   bobbyhadz
🌐
Codegive
codegive.com › blog › js_remove_non_alphanumeric_characters.php
JS Remove Non-Alphanumeric Characters: Master String Cleanup & Data Validation (2026 Ready!)
To remove all non-alphanumeric characters from a string in JavaScript, use the replace() method with a regular expression like /[^a-zA-Z0-9]/g. This pattern matches any character that is not an uppercase letter, lowercase letter, or a digit, and the g flag ensures all occurrences are replaced.
🌐
Codegive
codegive.com › blog › js_remove_non_alphanumeric.php
Master <code>js remove non alphanumeric</code> (2024): Clean Strings Like a Pro & Boost Your Data Integrity!
When we talk about "js remove non ... characters – are removed. In JavaScript, this is primarily achieved using regular expressions (regex) combined with the String.prototype.replace() method....
🌐
Codegive
codegive.com › blog › js_remove_non_alphanumeric_from_string.php
JS Remove Non-Alphanumeric from String: Master Data Cleanup in 2026 for Flawless Web Apps!
When we talk about "js remove non ... (depending on the requirement). In JavaScript, the primary tool for this operation is the String.prototype.replace() method, coupled with powerful Regular Expressions (Regex)....
🌐
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
The String.prototype.replace() method, combined with a simple regular expression, provides a powerful and concise way to remove non-alphanumeric characters from a string.