removing all non-letter characters from a string? ((using regex))
java - Regular Expression to remove everything but characters and numbers - Stack Overflow
regex - Remove all characters except - Code Review Stack Exchange
Regex to Remove Everything but Numbers, Letters and Spaces in R - Stack Overflow
You can use regex
myString.replace(/[^\w\s!?]/g,'');
This will replace everything but a word character, space, exclamation mark, or question.
Character Class:
\wstands for "word character", usually[A-Za-z0-9_]. Notice the inclusion of the underscore and digits.
\sstands 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.
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, '');
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
Given
s = '@#24A-09=wes()&8973o**_##me' # contains letters 'Awesome'
You can filter out non-alpha characters with a generator expression:
result = ''.join(c for c in s if c.isalpha())
Or filter with filter:
result = ''.join(filter(str.isalpha, s))
Or you can substitute non-alpha with blanks using re.sub:
import re
result = re.sub(r'[^A-Za-z]', '', s)
A solution using RegExes is quite easy here:
import re
newstring = re.sub(r"[^a-zA-Z]+", "", string)
Where string is your string and newstring is the string without characters that are not alphabetic. What this does is replace every character that is not a letter by an empty string, thereby removing it. Note however that a RegEx may be slightly overkill here.
A more functional approach would be:
newstring = "".join(filter(str.isalpha, string))
Unfortunately you can't just call stron a filterobject to turn it into a string, that would look much nicer...
Going the pythonic way it would be
newstring = "".join(c for c in string if c.isalpha())
whitelist = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ')
myStr = "happy t00 go 129.129$%^&*("
answer = ''.join(filter(whitelist.__contains__, myStr))
Output:
>>> answer
'happy t go '
Use a set complement:
re.sub(r'[^a-zA-Z ]+', '', 'happy t00 go 129.129')
Here is one way of doing it in Notepad++:
- Press Ctrl+M
- Set Search mode to Regular expression
- Find what :
\bSS(\d){4,8}$ - Click Mark All
- Click Copy Marked Text
- Open a new text file
- Press Ctrl+V to paste the copied text.

- Ctrl+F
- Find what:
\bSS(?:\d\d){2,4}\b - CHECK Match case
- CHECK Wrap around
- CHECK Regular expression
- Find All in Current Document
Explanation:
\b # word boundary
SS # literally SS
(?: # non capture group
\d\d # 2 digits
){2,4} # group may appear 2,3 or 4 times
\b # word boundary
Screenshot:

If you want to delete everything but SS..., use:
- Ctrl+H
- Find what:
^.*?\b(SS(?:\d\d){2,4})\b.*$ - Replace with:
$1 - CHECK Match case
- CHECK Wrap around
- CHECK Regular expression
- UNCHECK
. matches newline - Replace all
A carefully chosen regular expression can strip out the weightings in one replacement. A second switches the hyphens - for underscores _
const locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5';
newLocales = locales.replace(/;q=\d*\.\d*/g,'').replace(/-/g,'_');
console.log(newLocales); // fr_CH, fr, en, de, *
You could replace and search for two small characters and possible following dash and some more upper case characters or a star.
const
locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5',
parts = locales
.replace(/-/g, '_')
.match(/[a-z]{2}_*[A-Z]*|\*/g)
.join(', ');
console.log(parts);
An even shorter approach which relaces all parts after semicolon (inclusive) until following comma without converting/matching to an array an joining back to a string.
const
locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5',
parts = locales
.replace(/-/g, '_')
.replace(/;[^,]+/g, '')
console.log(parts);