You can use regex

myString.replace(/[^\w\s!?]/g,'');

This will replace everything but a word character, space, exclamation mark, or question.

Character Class: \w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.

\s stands for "whitespace character". It includes [ \t\r\n].

If you don't want the underscore, you can use just [A-Za-z0-9].

myString.replace(/[^A-Za-z0-9\s!?]/g,'');

For unicode characters, you can add something like \u0000-\u0080 to the expression. That will exclude all characters within that unicode range. You'll have to specify the range for the characters you don't want removed. You can see all the codes on Unicode Map. Just add in the characters you want kept or a range of characters.

For example:

myString.replace(/[^A-Za-z0-9\s!?\u0000-\u0080\u0082]/g,'');

This will allow all the previously mentioned characters, the range from \u0000-\u0080 and \u0082. It will remove \u0081.

Answer from sachleen 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

Top answer
1 of 4
58

You can use regex

myString.replace(/[^\w\s!?]/g,'');

This will replace everything but a word character, space, exclamation mark, or question.

Character Class: \w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.

\s stands for "whitespace character". It includes [ \t\r\n].

If you don't want the underscore, you can use just [A-Za-z0-9].

myString.replace(/[^A-Za-z0-9\s!?]/g,'');

For unicode characters, you can add something like \u0000-\u0080 to the expression. That will exclude all characters within that unicode range. You'll have to specify the range for the characters you don't want removed. You can see all the codes on Unicode Map. Just add in the characters you want kept or a range of characters.

For example:

myString.replace(/[^A-Za-z0-9\s!?\u0000-\u0080\u0082]/g,'');

This will allow all the previously mentioned characters, the range from \u0000-\u0080 and \u0082. It will remove \u0081.

2 of 4
7

Both answers posted so far left out the question mark. I would comment on them, but don't have enough rep yet.

David is correct, sachleen's regex will leave underscores behind. rcdmk's regex, modified as follows, will do the trick, although if you care about international characters things might get a lot more complicated.

var result = text.replace(/[^a-zA-Z0-9\s!?]+/g, '');

This will leave behind new lines and tabs as well as spaces. If you want to get rid of new lines and tabs as well, change it to:

var result = text.replace(/[^a-zA-Z0-9 !?]+/g, '');
Discussions

javascript - Regex: remove everything except the letters and separator - Stack Overflow
I am currently using replace statements to replace certain parts of a string. I think my code is a bit over the top and could be simplified: const locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;... More on stackoverflow.com
🌐 stackoverflow.com
April 16, 2021
What is the regex pattern to remove all characters except numbers and letters?
I am not good with regex. What is the pattern to remove all spaces, tabs, dashes, and all other non- number or letter characters easily from a string? More on forum.bubble.io
🌐 forum.bubble.io
2
1
January 23, 2020
javascript - Regular expression to remove anything but alphabets and '[single quote] - Stack Overflow
How can I change this regular expression to remove everything from a string except alphabets and a '(single quote)? pattern = /\b(ma?c)?([a-z]+)/ig; this pattern removes unwanted spaces and capit... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Regex to remove letters, symbols except numbers - Stack Overflow
How can you remove letters, symbols such as ∞§¶•ªºº«≥≤÷ but leaving plain numbers 0-9, I want to be able to not allow letters or certain symbols in an input field but to leave numbers only. Demo. ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-all-characters-except-numbres
Remove all non-numeric characters from String in JavaScript | bobbyhadz
The part between the square brackets [] is called a character class and matches everything except for digits and dots. The caret ^ symbol in the regex means "NOT the following".
🌐
The Web Dev
thewebdev.info › home › how to remove everything but letters, numbers, space, exclamation and question mark from a javascript string?
How to remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string? - The Web Dev
January 11, 2022 - To remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string, we can use the JavaScript string’s replace method. How to remove numbers from a string using JavaScript? Sometimes, we want to remove numbers from a string using JavaScript. In this article, we'll… ... To remove hyphens from a JavaScript string, we can call the JavaScript string’s replace method.…
🌐
Bubble
forum.bubble.io › need help
What is the regex pattern to remove all characters except numbers and letters? - Need help - Bubble Forum
January 23, 2020 - I am not good with regex. What is the pattern to remove all spaces, tabs, dashes, and all other non- number or letter characters easily from a string?
Top answer
1 of 1
8

To remove characters, you'd need to use something that actually does that, like the string replace function (which can accept a regular expression as the "from" parameter).

Then you're just dealing with a normal application of a character class, which in JavaScript (and most other regular expression variants) is described using [...], where ... is what should be in the class. You'd use the ^ at the beginning to invert the meaning of the class:

In your case, it might be:

str = str.replace(/[^A-Za-z']/g, "");

...which will replace except the English characters A-Z (ABCDEFGHIJKLMNOPQRSTUVWXYZ), a-z (abcdefghijklmnopqrstuvwxyz), and the single quote with nothing (e.g., remove it).

let str = "This is a test with the numbers 123 and a '.";

console.log("before:", str);
str = str.replace(/[^A-Za-z']/g, "");
console.log("after: ", str);

However, note that alphabetic characters not used in English will not be excepted, and there are a lot of those in the various languages used on the web (and even, perversely, in English, in "borrowed" words like "voilà" and "naïve").

You've said you're okay with just English A-Z, but for others coming to this: In environemnts supporting ES2018 and above's Unicode property matching, you could handle anything considered "alphabetic" by Unicode instead of just A-Z by using the \p{Alpha} property. The \p means "matching this Unicode property" (as usual, the lowercase version \p means "matching" and the uppercase version \P means "not matching") and the {Alpha} means "alphabetic":

str = str.replace(/[^\p{Alpha}']/gu, "");

(Note that, again, \p{Alpha} means "alphabetic" but because it's in a negated character class, we're excluding alphabetic characters.)

Note the u flag on that, to enable newer Unicode features. That handles the "voilà" and "naïve" examples too:

let str = "This is a test with the numbers 123 and a ' and voilà and naïve.";

console.log("before:", str);
str = str.replace(/[^\p{Alpha}']/gu, "");
console.log("after: ", str);

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-strip-all-non-numeric-characters-from-string
JavaScript - Strip All Non-Numeric Characters From String - GeeksforGeeks
July 11, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Bubble
forum.bubble.io › need help
How to remove anything except letters and numbers? - Need help - Bubble Forum
December 14, 2017 - Hey guys, I have one field like Name: Then I have another one like: Name2: I want to get the data that the user typed into Name field, and put it into Name2, but removing anything except letters (e.g.: special charact…
🌐
Stack Abuse
stackabuse.com › bytes › strip-non-numeric-characters-from-a-string-in-javascript
Strip Non-Numeric Characters from a String in JavaScript
September 12, 2023 - In this code snippet, we use the \D character in a regular expression (regex) to match any character that's not a digit. The g flag ("global") is used to match all occurances, as opposed to just the first one. Alternatively, you could also use the [^0-9] pattern to match any character that's ...