Regular Expression: Any character that is not a letter or number

JavascriptRegex

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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, ' ');

Solution 2 - Javascript

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"

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

  • 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

Solution 6 - Javascript

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

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

Solution 7 - Javascript

Just for others to see:

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

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

Source

Solution 8 - Javascript

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

Solution 9 - Javascript

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 = '§$)% #$@';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames JefferyView Question on Stackoverflow
Solution 1 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 2 - JavascriptsbmaxxView Answer on Stackoverflow
Solution 3 - JavascriptfavoView Answer on Stackoverflow
Solution 4 - Javascripttobi-or-notView Answer on Stackoverflow
Solution 5 - JavascriptYoussef AbouEglaView Answer on Stackoverflow
Solution 6 - JavascriptaayushiView Answer on Stackoverflow
Solution 7 - JavascriptRichieView Answer on Stackoverflow
Solution 8 - JavascriptaloisdgView Answer on Stackoverflow
Solution 9 - JavascriptJuan GaitánView Answer on Stackoverflow