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
🌐
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!!!

Discussions

Regex to remove special characters
Hi community Happy new year, I'm not to good with RegEx but I know it is needed in my use case. I have these company names but they contain special characters and i'm not sure how to remove them. I've attached a mini dummy sample of the data below. Any assistance with this would be majorly ... More on community.alteryx.com
🌐 community.alteryx.com
January 5, 2023
Get rid of special characters in string
Not a bubble question - a javastring question. Have some problems getting .replace to work when removing special characters from a string of special characters. For most special characters, just using the special character works. For others like $ or ^, using the special character doesn’t work. More on forum.bubble.io
🌐 forum.bubble.io
13
0
July 1, 2023
Remove special characters from string using regexp_replace - Oracle Forums
Hi, I'm writing a function to remove special characters and non-printable characters that users have accidentally entered into CSV files. I've looked at the ASCII character map, and basically, for eve... More on forums.oracle.com
🌐 forums.oracle.com
December 5, 2007
Removing all right of special Characters with Regex
Hi, I tried to fix this myself but am running into some issues. I want to delete all text to the right of some special Characters ( "," "/" "-" - etc) This one \..*$ works at regexr.com but not in Keyboard Maestro (as an Example for ".") The intent is to have text that is like XXX.YYY XXX - ... More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
0
0
January 1, 2024
People also ask

How to remove specific characters from a string in JavaScript?
To remove specific characters from a string, you can use the .replace() method with a regular expression. Code: let str = "Hello, World!"; let result = str.replace(/[,!]/g, ''); console.log(result); Output: Hello World
🌐
intellipaat.com
intellipaat.com › home › blog › remove all special characters except space from a string using javascript
Remove all special characters except space from a string using ...
How to remove space from a string in JavaScript?
To remove space from a string in JavaScript, you can use the .replace() method with a regular expression: Code: let str = "Hello World"; let result = str.replace(/s+/g, ''); console.log(result); Output: HelloWorld
🌐
intellipaat.com
intellipaat.com › home › blog › remove all special characters except space from a string using javascript
Remove all special characters except space from a string using ...
How to give space in a string in JavaScript?
We can add space in a string using concatenation or template literals. Code: let str1 = "Hello"; let str2 = "World"; let result = str1 + " " + str2; console.log(result); let result2 = `${str1} ${str2}`; console.log(result2); Output: Hello World Hello World
🌐
intellipaat.com
intellipaat.com › home › blog › remove all special characters except space from a string using javascript
Remove all special characters except space from a string using ...
🌐
UiPath Community
forum.uipath.com › help
How do i remove special characters from a string? - Help - UiPath Community Forum
January 27, 2020 - hi all, i have seen it been done using REGEX but i would like to know how to do it without as mine doesn’t appear to be working Thank you
🌐
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.
Find elsewhere
🌐
ServiceNow Community
servicenow.com › community › developer-forum › using-regex-to-make-special-characters-disappear-except-when-it › m-p › 2574305
Using regex to make special characters disappear except when it is a dash. How do you do that?
May 31, 2023 - Correct. All the '-' signs are removed earlier within your step 1, since these are considered to be special characters. You can modify your step 1 and include '-' in your existing regex.
🌐
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(/^\.+/, ''
🌐
Bubble
forum.bubble.io › need help
Get rid of special characters in string - Need help - Bubble Forum
July 1, 2023 - Not a bubble question - a javastring question. Have some problems getting .replace to work when removing special characters from a string of special characters. For most special characters, just using the special character works. For others like $ or ^, using the special character doesn’t work.
🌐
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 - We use Character1 & Character2 ... each. Special characters like backslashes need to be ‘escaped’ by preceding them with another backslash as the Regex string uses a backslash as a functional character....
🌐
TutorialsPoint
tutorialspoint.com › article › javascript-regex-how-to-replace-special-characters
JavaScript regex - How to replace special characters?
March 15, 2026 - Use /[^a-zA-Z0-9]/g to remove all special characters or create custom patterns for specific requirements.
🌐
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.
🌐
Oracle
forums.oracle.com › ords › apexds › post › remove-special-characters-from-string-using-regexp-replace-5185
Remove special characters from string using regexp_replace - Oracle Forums
December 5, 2007 - Hi, I'm writing a function to remove special characters and non-printable characters that users have accidentally entered into CSV files. I've looked at the ASCII character map, and basically, for eve...
🌐
Keyboard Maestro
forum.keyboardmaestro.com › questions & suggestions
Removing all right of special Characters with Regex - Questions & Suggestions - Keyboard Maestro Discourse
January 1, 2024 - Hi, I tried to fix this myself but am running into some issues. I want to delete all text to the right of some special Characters ( "," "/" "-" - etc) This one \..*$ works at regexr.com but not in Keyboard Maestro (as …
🌐
Medium
medium.com › @vidvatek › how-to-remove-special-characters-from-string-in-python-13b04516421a
How to Remove Special Characters from String in Python | Medium
December 15, 2023 - The regular expression [^\w\s] matches any character that is not a word character (\w, which includes letters, digits, and underscores) or a whitespace character (\s). The ^ inside the square brackets negates the character class, meaning it matches any character that is not in the specified set. ... This code demonstrates how to remove special characters from a string using the re.sub() function and regular expressions in Python.
🌐
Ablebits
ablebits.com › ablebits blog › excel › regex › regex to remove certain characters or text in excel
Regex to remove certain characters or text in Excel
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.
🌐
LazyWinAdmin
lazywinadmin.com › 2015 › 08 › powershell-remove-special-characters.html
PowerShell - Remove special characters from a string using Regular Expression (Regex) - LazyWinAdmin
August 30, 2015 - # Regular Expression - Unicode - Unicode Categories # Exceptions: We want to keep the following characters: ( } _ $String -replace '[^\p{L}\p{Nd}/(/}/_]', '' ... function Remove-StringSpecialCharacter { <# .SYNOPSIS This function will remove the special character from a string.
🌐
Quora
quora.com › Can-special-characters-be-removed-from-a-string-using-regex
Can special characters be removed from a string using regex? - Quora
Answer: There are already direct answers and yes of course Nginx can do this. However, one should note something highly special about Nginx configs and Runtime… If you are familiar with the concept “ABC is a first-class citizen in XYZ” many people are not aware that values are first-class ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex replace special characters
Python regex replace special characters - Spark By {Examples}
May 31, 2024 - How to replace special characters in Python using regex? As you are working with strings, you might find yourself in a situation where you want to replace
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
May 25, 2026 - The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation. It is important to note that most regular expression operations are available as module-level functions and methods on compiled regular expressions. The functions are shortcuts that don’t require you to compile a regex object first, but miss some fine-tuning parameters.
🌐
Intellipaat
intellipaat.com › home › blog › remove all special characters except space from a string using javascript
Remove all special characters except space from a string using JavaScript - Intellipaat
February 3, 2026 - So far in this article, we have learned how we can remove all special characters except space from a string using regular expressions in JavaScript. We have to use replace() function and match() to do this task.