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
🌐
Text-Utils
text-utils.com › remove special characters
Remove Special Characters - Online Regex Tools | Text-Utils.com
September 6, 2025 - Remove Special Characters online: The tool can be used to remove special characters from text. Keep alphanumeric & spaces only: Remove all non-alphanumeric & and non-spaces characters from the text.
Discussions

javascript - Remove not alphanumeric characters from string
The following is the/a correct regex to strip non-alphanumeric chars from an input string: ... Note that \W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.: More on stackoverflow.com
🌐 stackoverflow.com
How can I remove all NON alphabetic characters from my list of strings [PYTHON]

Something like that is perfect for regular expressions. re.sub takes as input a regular expression that defines what to match, a string (or a function) to decide what to replace what is matched with, and a string to do all this matching and replacing on. As an example:

import re
string = "lincoln's silly flat dishwatery utterances chicago times 1863"
print re.sub(r'[^a-zA-Z ]', '', string).split()

# ouputs:
# ['lincolns', 'silly', 'flat', 'dishwatery', 'utterances', 'chicago', 'times'] 

This will replace all characters that are not between lowercase a-z, uppercase A-Z, or a space, with nothing -- essentially removing them.

More on reddit.com
🌐 r/learnprogramming
5
1
March 11, 2015
c# - How do I remove all non alphanumeric characters from a string except dash? - Stack Overflow
How do I remove all non alphanumeric characters from a string except dash and space characters? More on stackoverflow.com
🌐 stackoverflow.com
Stripping everything but alphanumeric chars from a string in Python - Stack Overflow
I just timed some functions out of curiosity. In these tests I'm removing non-alphanumeric characters from the string string.printable (part of the built-in string module). More on stackoverflow.com
🌐 stackoverflow.com
🌐
CleanYourText
cleanyourtext.com › remove-special-characters
Remove Special Characters Online - CleanYourText
Clean your text by instantly removing all symbols and non-alphanumeric characters.Use Remove Special Characters Tool
🌐
Web Carpenter
webcarpenter.com › blog › 40-Php-Remove-Non-Alphanumeric-Characters
Php Remove Non-Alphanumeric Characters // Web Carpenter
How to strip all symbols and numbers from a string of alphanumeric text. This php code will help you delete, remove, strip and erase any non-alphanumeric characters, and then return the data without the unwanted characters.
Find elsewhere
🌐
Flexiple
flexiple.com › python › remove-non-alphanumeric-characters-python
Removing Non-Alphanumeric Characters in Python - Flexiple
March 21, 2024 - It includes only those characters in clean_text that are alphanumeric. The result is a string free of spaces, punctuation, and special characters. This approach is particularly useful for basic text-cleaning tasks in data processing and text analysis applications. Using regular expressions in Python offers a powerful and flexible approach to removing non-alphanumeric characters from strings.
🌐
Reddit
reddit.com › r/learnprogramming › how can i remove all non alphabetic characters from my list of strings [python]
r/learnprogramming on Reddit: How can I remove all NON alphabetic characters from my list of strings [PYTHON]
March 11, 2015 -

FYI I do want to keep the commas between strings in the list. Here is my code right now. I've been working on this for hours and I'm stuck, I can't figure it out. I working on a program that will append every string of at least four characters to a new list. I'm going to use this to run through some ebooks I have in .txt

def parse(s):
    listOfWords = []
    i = 0
    final = s.lower()
    final = final.split()
    while i < len(final):
        if len(final[i]) >= 4:
                listOfWords.append(final[i])
        i = i + 1
    return listOfWords

Current Output

['lincoln’s', 'silly,', 'flat', 'dishwatery', 'utterances', 'chicago',    'times,', '1863']

Desired Output

["lincoln", "silly", "flat", "dishwatery", "utterances", "chicago", "times"]
🌐
Gopher Coding
gophercoding.com › remove-non-alphanumeric-chars
Remove All Non-Alphanumeric Characters · Gopher Coding
April 2, 2023 - <p>We often need to remove symbols and special characters from the strings we&rsquo;re using (<em>especially with currency!</em>). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in &ldquo;$1,000&rdquo; you can turn it into &ldquo;1000&rdquo;.</p> <p>We use the <code>regexp</code> package to do this, first building a regex with <code>.Compile()</code> then running the string through that regex with <code>.ReplaceAllString()</code>. Finally with display and compare both strings.</p>
🌐
Regex101
regex101.com › r › jI5hK6 › 1
regex101: Remove Non-Alphanumeric Characters
Any whitespace character · \s · Any non-whitespace character · \S · Any digit · \d · Any non-digit · \D · Any word character · \w · Any non-word character · \W · Non-capturing group · (?:...) Capturing group · (...) Zero or one of a · a? Zero or more of a ·
🌐
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.
🌐
gosamples
gosamples.dev › tutorials › remove non alphanumeric characters from a string in go
🧽 Remove non-alphanumeric characters from a string in Go
May 19, 2022 - To clear a string from all non-alphanumeric characters in Go, it is best to use a regular expression that matches non-alphanumeric characters, and replace them with an empty string. Such a regex is defined as the negation of the set of allowed characters.
🌐
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.
🌐
PhraseFix
phrasefix.com › tools › remove-special-characters
Clean Special Characters from Text - PhraseFix
Use this tool to remove special characters (i.e. Exclamation mark, Quotation mark, Number sign, Dollar sign, Slashes) and keep only alphanumeric characters.
🌐
TextTools.org
texttools.org › remove-unwanted-characters
Remove Unwanted Characters - Text Tools
Just type the characters you want to be removed in the text box and click remove. No need to add separators like commas, or escape characters. There are also several templates readily available for you to use. ... All non-alphanumeric - Deletes any character/symbol that is not a number or letter ...
🌐
Cloverdx
doc.cloverdx.com › 6.2.2 › wrangler › wrangler-step-remove-non-alphanumeric.html
Remove non-alphanumeric characters | CloverDX 6.2.2 Documentation
Remove non-alphanumeric characters removes whitespaces, punctuation, math operators and other special characters from the text, only letters and numbers are kept.