Replace [^a-zA-Z0-9 -] with an empty string.

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");
Answer from Amarghosh on Stack Overflow
๐ŸŒ
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

Discussions

Regular Expression: Any character that is not a letter or number
I believe he is looking for /(_|\W)/g, ... digit or letter (english language) 2010-06-07T18:44:31.45Z+00:00 ... @sbmaxx I want to replace all except &, (, ) these characters. how could i add this condition in the current regex. 2019-02-21T11:24:01.67Z+00:00 ... Save this answer. ... Show activity on this post. ... This replaces all non-alphanumeric ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Replace all non alphanumeric characters, new lines, and multiple white space with one space
I'm looking for a neat regex solution to replace All non alphanumeric characters All newlines All multiple instances of white space With a single space For those playing at home (the following d... More on stackoverflow.com
๐ŸŒ stackoverflow.com
RegEx to remove all non alphanumeric characters
Could someone please show me how to use some simple RegEx to do the following please: "String with spaces, punctuation; and numbers (22)" -> "Stringwithspacespunctuationandnumbers22" The only characters I want to retain are letters a-z (case doesn't matter) and numbers 0 to 9. I'm working with ... More on community.alteryx.com
๐ŸŒ community.alteryx.com
December 1, 2016
neovim - Regex for any character that is not alphanumeric, whitespace, or a doublequote - Vi and Vim Stack Exchange
I expect this to select all non-alphanumeric characters: [^\w] Instead it selects all non-w's. This is the regex I would use in JavaScript, Python etc.: [^\w\s"] How can I write this so that... More on vi.stackexchange.com
๐ŸŒ vi.stackexchange.com
November 2, 2020
๐ŸŒ
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 replace() method with the regular expression /[^a-zA-Z0-9\s]/g removes all characters that are not letters, numbers, or spaces. The ^ inside the brackets creates a negated character class, meaning it matches anything NOT in the specified range.
๐ŸŒ
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 - Look at the output and notice that this method removes both non-English letters (ู€ุง, ฤ…) and numbers (ูฆ). package main import ( "fmt" "regexp" ) var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`) func clearString(str string) string { return nonAlphanumericRegex.ReplaceAllString(str, "") } func main() { str := "Test@%String#321gosamples.dev ู€ุง ฤ… ูฆ" fmt.Println(clearString(str)) } ... This example works like the previous one, but by using a regular expression with Unicode categories, we also accept letters and numbers from alphabets other than English, such as Arabic.
๐ŸŒ
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 - We will explore all the above methods along with their basic implementation with the help of examples. Regular expressions offer a concise way to match and remove non-alphanumeric characters. We can use the replace() method with a regular expression to replace all non-alphanumeric characters with an empty string.
๐ŸŒ
Davidsonsousa
davidsonsousa.net โ€บ blog โ€บ post โ€บ removing-all-non-numeric-characters-from-a-string-using-regex
Replacing all non numeric characters from a string using Regex - Davidson Sousa
While in the user interface we should allow dots and dashes as the user will never really care if he should or should not use it. In Brazil, for example, the postal code has the following format: 00000-000. Some people might put it without the dash. And that, my friend, will hurt the database. One way to solve this is forcing the backend to keep only the numbers. Let's take a look on how to do it using Regex.Replace:
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-characters-not-match-regex
Replace/Remove characters that Don't match Regex in JS | bobbyhadz
... Copied!const str = 'abc123'; const result = str.replace(/[^a-z]/g, '!'); console.log(result); // ๐Ÿ‘‰๏ธ "abc!!!" We used the g (global) flag because we want to match all occurrences of a non-lowercase, Latin letter, and not just the first ...
๐ŸŒ
Medium
medium.com โ€บ @shamzaibrahim7 โ€บ three-techniques-to-remove-non-alphanumeric-characters-for-palindrome-problem-9ab7a78d8490
Three Techniques to Remove Non-Alphanumeric Characters for Palindrome Problem | by Syed Hamza | Medium
March 27, 2023 - Here is an example of how to use re.sub to replace all occurrences of the pattern "dog" with the replacement string "cat" in a string: ... Another way to remove non-alphanumeric characters is by checking the ASCII value of each character in the given phrase. If the ASCII value of a character is between 48 and 57 (inclusive) or between 97 and 122 (inclusive), it is an alphanumeric character.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - The string can be easily filtered using the ReGex [^a-zA-Z0-9 ]. ... // Java program to remove non-alphanumeric // characters from a string class GFG { // Main driver method public static void main(String args[]) { // Input string String str1 ...
๐ŸŒ
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; ... This will remove all the non-alphanumeric characters from the string and replaces it with ...
๐ŸŒ
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>
๐ŸŒ
Microsoft
devblogs.microsoft.com โ€บ dev blogs โ€บ scripting blog [archived] โ€บ powertip: replace non-alphabetic characters in string
PowerTip: Replace Non-Alphabetic Characters in String - Scripting Blog [archived]
February 18, 2019 - How can I use Windows PowerShell ... and specify a regex pattern โ€˜[^a-zA-Z]โ€™, for example: $a = โ€˜Never a foot too far, even.โ€™ $a -Replace โ€˜[^a-zA-Z]โ€™,โ€...
๐ŸŒ
Regex101
regex101.com โ€บ r โ€บ jI5hK6 โ€บ 1
regex101: Remove Non-Alphanumeric Characters
An explanation of your regex will be automatically generated as you type.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.text.regularexpressions.regex.replace
Regex.Replace Method (System.Text.RegularExpressions) | Microsoft Learn
June 7, 2023 - The recommended static method for replacing a pattern match is Replace(String, String, String, RegexOptions, TimeSpan), which lets you set the time-out interval. ... In a specified input string, replaces all strings that match a specified regular ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-non-alphanumeric-characters-from-string
Remove all non-alphanumeric Characters from a String in JS | bobbyhadz
Copied!const str = 'A!@# b$% ^c&-*('; const replaced = str.replace(/[^a-z0-9 -]/gi, ''); console.log(replaced); // ๐Ÿ‘‰๏ธ A b c- The code sample preserves all alphanumeric characters, spaces and hyphens.