167

I need a regular expression that will match any character that is not a letter or a number. Once found I want to replace it with a blank space.

11 Answers 11

213

To match anything other than letter or number you could try this:

[^a-zA-Z0-9]

And to replace:

var str = 'dfj,dsf7lfsd .sdklfj';
str = str.replace(/[^A-Za-z0-9]/g, ' ');
5
  • 26
    \w is for Word characters and is exactly the same as [a-zA-Z0-9_] (notice that underscore is considered a word character.) ...so the shorthand would be str.replace(/[^\w]/g, ' ') Commented Aug 30, 2013 at 16:41
  • but it will include many unicode letters, too! is there any way to exclude unicode letters? Commented May 4, 2017 at 12:06
  • 2
    To include unicode characters, you can use [^\p{L}0-9]
    – Dave
    Commented Aug 9, 2017 at 21:29
  • 1
    @Dave: As of 2018 you can't without a polyfill, apparently... stackoverflow.com/questions/280712/javascript-unicode-regexes
    – Nickolay
    Commented Jun 7, 2018 at 16:39
  • is there a way, instead of repalce with a space... replace with "\" followed by the character that was been identified? Like this: make this dfj,dsf7lfsd .sdklfj into this dfj\,dsf7lfsd \.sdklfj?
    – CrazySpy
    Commented Nov 26, 2019 at 13:52
64

This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.

\W

For example in JavaScript:

"(,,@,£,() asdf 345345".replace(/\W/g, ' '); // Output: "          asdf 345345"
2
  • I believe he is looking for /(_|\W)/g, to match anything not a digit or letter (english language)
    – kennebec
    Commented Jun 7, 2010 at 18:44
  • @sbmaxx I want to replace all except &, (, ) these characters. how could i add this condition in the current regex.
    – Kunal Pal
    Commented Feb 21, 2019 at 11:24
18

You are looking for:

var yourVar = '1324567890abc§$)%';
yourVar = yourVar.replace(/[^a-zA-Z0-9]/g, ' ');

This replaces all non-alphanumeric characters with a space.

The "g" on the end replaces all occurrences.

Instead of specifying a-z (lowercase) and A-Z (uppercase) you can also use the in-case-sensitive option: /[^a-z0-9]/gi.

2
  • Will it match spaces aswell? I need spaces to be kept. Thanks. Commented Jun 7, 2010 at 18:17
  • Space characters would match, but then would be replaced by space characters, so in effect it would leave them alone (a space will stay a space).
    – jimbo
    Commented Jun 7, 2010 at 18:42
16
  • Match letters only /[A-Z]/ig
  • Match anything not letters /[^A-Z]/ig
  • Match number only /[0-9]/g or /\d+/g
  • Match anything not number /[^0-9]/g or /\D+/g
  • Match anything not number or letter /[^A-Z0-9]/ig

There are other possible patterns

14

This is way way too late, but since there is no accepted answer I'd like to provide what I think is the simplest one: \D - matches all non digit characters.

var x = "123 235-25%";
x.replace(/\D/g, '');

Results in x: "12323525"

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

5

Just for others to see:

someString.replaceAll("([^\\p{L}\\p{N}])", " ");

will remove any non-letter and non-number unicode characters.

Source

2
  • I'm not sure where the two upvotes came from, but in JavaScript (which this question is about) there's no replaceAll, and the \p{..} property escapes are not widely implemented.
    – Nickolay
    Commented Jun 7, 2018 at 16:43
  • 1
    @Nickolay There is now! :)
    – leo
    Commented May 15, 2023 at 9:43
5

try doing str.replace(/[^\w]/); It will replace all the non-alphabets and numbers from your string!

Edit 1: str.replace(/[^\w]/g, ' ')

1
  • 2
    A working answer would be str.replace(/[^\w]/g, ' '). If you don't include /g flag it will only replace the first occurence. And if you don't define a replacement string, here a blank space ' ', it will replace by undefined all over the place. Finally, underscores will not be replaced because they match \w. This answer is not a perfect fit. Commented Feb 18, 2017 at 17:25
4

To match anything other than letter or number or letter with diacritics like é you could try this:

[^\wÀ-úÀ-ÿ]

And to replace:

var str = 'dfj,dsf7é@lfsd .sdklfàj1';
str = str.replace(/[^\wÀ-úÀ-ÿ]/g, '_');

Inspired by the top post with support for diacritics

source

2

Have you tried str = str.replace(/\W|_/g,''); it will return a string without any character and you can specify if any especial character after the pipe bar | to catch them as well.

var str = "1324567890abc§$)% John Doe #$@'.replace(/\W|_/g, ''); it will return str = 1324567890abcJohnDoe

or look for digits and letters and replace them for empty string (""):

var str = "1324567890abc§$)% John Doe #$@".replace(/\w|_/g, ''); it will return str = '§$)% #$@';

0

Working with unicode, best for me:

text.replace(/[^\p{L}\p{N}]+/gu, ' ');
0

To match anything other than letter or number you could try this:

import re

regular_expression = r'[^a-zA-Z0-9\s]'  
new_string = re.sub(regular_expression, '', old_string)

re substring is an alternative to the replace command but the key here is the regular expression.

The ^ means italic NOT ONE of the character contained between the brackets.

The /s _italic_means ANY ONE space/non-space character.

I recommend this web which explains regular expressions arguments: https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

2
  • You might want to add some context to your answer, what is this code doing?
    – mozway
    Commented Oct 24, 2023 at 11:43
  • Hope its a better answer now :) Commented Oct 25, 2023 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.