views:

77

answers:

3

I'm trying to figure out the regular expression that will match any character that is not a letter or a number. So characters such as (,,@,£,() etc ...

Once found I want to replace it with a blank space.

Any advice.

+7  A: 

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, ' ');
Darin Dimitrov
+4  A: 

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 occurences.

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

favo
Will it match spaces aswell? I need spaces to be kept. Thanks.
James Jeffery
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).
jimbojw
+2  A: 

This regular expression match not letters, digits, and underscores chars.


\W

For example in javascript:


"(,,@,£,() asdf 345345".replace(/\W/g, ' ');

sbmaxx
I believe he is looking for /(_|\W)/g, to match anything not a digit or letter (english language)
kennebec
... or /[\W_]/ perhaps
Pointy