Use the global flag:

var name = name.replace(/[^a-zA-Z ]/g, "");
                                    ^

If you don't want to remove numbers, add it to the class:

var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
Answer from Jerry on Stack Overflow
🌐
Ablebits
ablebits.com › ablebits blog › excel › regex › regex to remove certain characters or text in excel
Excel Regex to remove certain characters or text from strings
August 22, 2023 - To remove all matches, the instance_num argument is not defined: ... To strip off certain characters from a string, just write down all unwanted characters and separate them with a vertical bar | which acts as an OR operator in regexes.
Discussions

regex - Efficiently removing specific characters (some punctuation) from Strings in Java? - Stack Overflow
this will also remove spaces... and probably many other characters that OP doesn't want to remove like " 2013-07-08T16:21:17.123Z+00:00 ... \W stands for Anything that isn't a letter, number or underscore !!!It would be better if you can make the regex more specific . More on stackoverflow.com
🌐 stackoverflow.com
Regex remove some characters from string
Hi there! I am trying to clean up some data and I need to remove some characters that almost always appear in some of my rows. Here you have a short example: The column objective would be “Emoji Free”. For the removal of the emoji in “Name” I used this handy component: String Emoji ... More on forum.knime.com
🌐 forum.knime.com
1
0
August 13, 2021
c# - Regex for removing only specific special characters from string - Stack Overflow
I'd like to write a regex that would remove the special characters on following basis: To remove white space character @, &, ', (, ), or # I have written this regex which removes More on stackoverflow.com
🌐 stackoverflow.com
Using regex to remove non-alphabetical characters in a string.
if a pattern matches things you want to keep, like "all letters": [a-zA-Z] you can invert it with "^" to make it "not" those things: [^a-zA-Z] also, there are (depending on which language and which regex variant you're using) shortcuts like \s = a space (or tab or newline or...) \S = not a space \w = a word type character (i.e. a-z A-Z 0-9 etc) \W = not a word type character \d = a number \D = not a number More on reddit.com
🌐 r/learnprogramming
7
1
July 24, 2021
🌐
CoreUI
coreui.io › answers › how-to-remove-special-characters-from-a-string-in-javascript
How to remove special characters from a string in JavaScript · CoreUI
May 20, 2026 - The transliteration map converts accented characters to their ASCII equivalents before the regex strips anything remaining. For more on string replacement, see how to replace all occurrences of a string in JavaScript. File systems have strict rules about allowed characters. Sanitizing filenames prevents errors and security issues when saving user-provided names. // Remove characters not allowed in most file systems const sanitizeFilename = (filename) => { return filename .replace(/[<>:"/\\|?*\x00-\x1F]/g, '') // forbidden chars .replace(/\s+/g, '_') // spaces to underscores .replace(/^\.+/, ''
Top answer
1 of 8
18

Although \\p{Punct} will specify a wider range of characters than in the question, it does allow for a shorter replacement expression:

tmp = tmp.replaceAll("\\p{Punct}+", "");
2 of 8
12

Here's a late answer, just for fun.

In cases like this, I would suggest aiming for readability over speed. Of course you can be super-readable but too slow, as in this super-concise version:

private static String processWord(String x) {
    return x.replaceAll("[][(){},.;!?<>%]", "");
}

This is slow because everytime you call this method, the regex will be compiled. So you can pre-compile the regex.

private static final Pattern UNDESIRABLES = Pattern.compile("[][(){},.;!?<>%]");

private static String processWord(String x) {
    return UNDESIRABLES.matcher(x).replaceAll("");
}

This should be fast enough for most purposes, assuming the JVM's regex engine optimizes the character class lookup. This is the solution I would use, personally.

Now without profiling, I wouldn't know whether you could do better by making your own character (actually codepoint) lookup table:

private static final boolean[] CHARS_TO_KEEP = new boolean[];

Fill this once and then iterate, making your resulting string. I'll leave the code to you. :)

Again, I wouldn't dive into this kind of optimization. The code has become too hard to read. Is performance that much of a concern? Also remember that modern languages are JITted and after warming up they will perform better, so use a good profiler.

One thing that should be mentioned is that the example in the original question is highly non-performant because you are creating a whole bunch of temporary strings! Unless a compiler optimizes all that away, that particular solution will perform the worst.

🌐
Coding Beauty
codingbeautydev.com › home › posts › how to remove special characters from a string in javascript
How to Remove Special Characters From a String in JavaScript
October 13, 2022 - To remove all special characters from a string, call the replace() method on the string, passing a whitelisting regex and an empty string as arguments, i.e., str.replace(/^a-zA-Z0-9 ]/g, '').
🌐
C# Corner
c-sharpcorner.com › blogs › replace-special-characters-from-string-using-regex1
Replace Special Characters from string using Regex
February 18, 2015 - Regex.Replace(your String, @"[^0-9a-zA-Z]+", "") This code will remove all of the special characters but if you doesn't want to remove some of the special character for e.g.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-regex-from-string
Remove characters matching Regex from a String in Python | bobbyhadz
April 9, 2024 - Use the re.sub() method to remove the characters that match a regex from a string.
🌐
Reddit
reddit.com › r/learnprogramming › using regex to remove non-alphabetical characters in a string.
r/learnprogramming on Reddit: Using regex to remove non-alphabetical characters in a string.
July 24, 2021 -

Hi, I am trying to remove non-alphabetical characters from the following string.

let string = "Anne, I vote more cars race Rome-to-Vienna"

I can get the result I am looking for with the following...

let newString = string.toLowerCase().split('').filter(el => el !== ' ' && el !== '-' && el !== ',').join('')

However I would prefer to use a much shorter piece of code using regex. I have gone over a few regex examples and tutorials but it still kind of confused me. Does anybody know the simplest way to remove the unwanted characters from the above string?

Thanks!!!

🌐
Regex101
regex101.com › r › gJ6oZ6 › 1
regex101: Remove all special chars from string
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-special-characters-from-string
Remove special Characters from a String in JavaScript | bobbyhadz
Note that the caret ^ symbol has ... characters". If you use the caret ^ symbol later on in the regex, it will get interpreted as a literal caret ^ symbol. Note that the String.replace() method doesn't change the original string.
🌐
Laserfiche Answers
answers.laserfiche.com › questions › 71779 › What-is-the-regular-expression-to-remove-the-string-abc-from-a-token
What is the regular expression to remove the string "abc\" from a token? - Laserfiche Answers
February 19, 2015 - In the suggested solution, (\S+) worked because that matches "anything other than a space character". That means the suggested solution would also match abc\#$%^& by trimming the abc\. This might be what you want, but it might also not. From your description, the regex you'd most likely want would be
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-removing-unwanted-characters-from-string
Remove Special Characters from String in Python - GeeksforGeeks
July 11, 2025 - Let's explore different methods to achieve this. re.sub() function from re module allows you to substitute parts of a string based on a regex pattern. By using a pattern like [^a-zA-Z0-9], we can match and remove all non-alphanumeric characters.
🌐
iAm_ManCat Blog
iammancat.dev › home › remove characters from strings using regex in power apps
Remove characters from strings using Regex in Power Apps - iAm_ManCat Blog
March 17, 2023 - This can be used to validate and change strings based on the rules set out by the Regex string. In this example, We start by defining a group with an open round bracket, then we define a character section using a square bracket, then I am only going to allow Characters A-Z (Uppercase letters) and Characters a-z (lowercase letters) and Characters 0-9 (all numbers) and the ‘space’ character, then we close that section with a close square bracket and close the group with a close round bracket. ... If we want to remove characters from strings but have it done automatically, there are a few ways to do this.
🌐
regex101
regex101.com › library › UwbODR
regex101: Remove characters after a certain character
This regex matches only when all the following are true: password must contain 1 number (0-9) password must contain 1 uppercase letters password must contain 1 lowercase letters password must contain 1 non-alpha numeric number password is 8-16 characters with no spaceSubmitted by qho
🌐
UiPath Community
forum.uipath.com › help › studio
How to remove a special characters in a given string by regex - Studio - UiPath Community Forum
September 27, 2023 - how to remove a special characters in a given string by regex? “philippines nt., (Roosevelt Ave 3), St@a. philippines City, @U/SA”
🌐
Text-Utils
text-utils.com › remove special characters
Remove Special Characters - Online Regex Tools | Text-Utils.com
September 6, 2025 - Keep numeric & spaces only: Remove all non-numeric & non-space characters from the text.