正则表达式:任何不是字母或数字的字符

时间:2022-04-29 21:45:00

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.

任何建议。

8 个解决方案

#1


111  

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

#2


24  

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

这个正则表达式不匹配字母、数字和下划线。


\W

For example in javascript:

例如在javascript中:


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

#3


13  

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.

末尾的“g”取代了所有出现的情况。

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

而不是明确的“a - z(小写)和a - z(大写)你也可以使用in-case-sensitive选项:/[^ a-z0-9]/ gi。

#4


7  

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.

这已经太迟了,但是由于没有公认的答案,我想提供我认为最简单的答案:\D -匹配所有非数字字符。

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

Results in x: "12323525"

结果在x:“12323525”

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

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

#5


2  

Just for others to see:

只为给别人看:

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

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

将删除任何非字母和非数字unicode字符。

Source

#6


1  

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.

是否尝试过str = str.replace(/\W|_/g);它将返回一个没有任何字符的字符串,您可以指定管道条|之后的任何特殊字符是否也可以捕获它们。

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

var str = " 1324567890美元abc§)% John Doe # $ @”。替换(\ W | _ / g,”);它将返回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 = '§$)% #$@';

var str = " 1324567890美元abc§)% # $ @ John Doe”。替换(\ w | _ / g,”);它将返回str = '§$)% # $ @ ';

#7


0  

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

尝试str.replace(/[w ^ \]);它将取代所有非字母表和数字从你的字符串!

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

编辑1:str.replace(/ ^ \ w / g,' ')

#8


0  

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

要匹配除字母、数字或字母以外的任何字符,比如e,你可以试试以下方法:

[^\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

#1


111  

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

#2


24  

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

这个正则表达式不匹配字母、数字和下划线。


\W

For example in javascript:

例如在javascript中:


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

#3


13  

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.

末尾的“g”取代了所有出现的情况。

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

而不是明确的“a - z(小写)和a - z(大写)你也可以使用in-case-sensitive选项:/[^ a-z0-9]/ gi。

#4


7  

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.

这已经太迟了,但是由于没有公认的答案,我想提供我认为最简单的答案:\D -匹配所有非数字字符。

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

Results in x: "12323525"

结果在x:“12323525”

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

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

#5


2  

Just for others to see:

只为给别人看:

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

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

将删除任何非字母和非数字unicode字符。

Source

#6


1  

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.

是否尝试过str = str.replace(/\W|_/g);它将返回一个没有任何字符的字符串,您可以指定管道条|之后的任何特殊字符是否也可以捕获它们。

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

var str = " 1324567890美元abc§)% John Doe # $ @”。替换(\ W | _ / g,”);它将返回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 = '§$)% #$@';

var str = " 1324567890美元abc§)% # $ @ John Doe”。替换(\ w | _ / g,”);它将返回str = '§$)% # $ @ ';

#7


0  

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

尝试str.replace(/[w ^ \]);它将取代所有非字母表和数字从你的字符串!

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

编辑1:str.replace(/ ^ \ w / g,' ')

#8


0  

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

要匹配除字母、数字或字母以外的任何字符,比如e,你可以试试以下方法:

[^\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