Javascript 正则表达式:任何不是字母或数字的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2991901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression: Any character that is NOT a letter or number
提问by James Jeffery
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.
任何建议。
回答by Darin Dimitrov
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, ' ');
回答by sbmaxx
This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.
此正则表达式匹配任何不是字母、数字或下划线 ( _) 字符的内容。
\W
For example in JavaScript:
例如在 JavaScript 中:
"(,,@,£,() asdf 345345".replace(/\W/g, ' '); // Output: " asdf 345345"
回答by favo
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.
除了指定 az(小写)和 AZ(大写),您还可以使用区分大小写的选项:/[^a-z0-9]/gi。
回答by tobi-or-not
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
回答by aayushi
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, ' ')
回答by Richie
回答by Juan Gaitán
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 = "1324567890abc§$)% John Doe #$@'.replace(/\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 = "1324567890abc§$)% John Doe #$@".replace(/\w|_/g, '');它会回来 str = '§$)% #$@';
回答by aloisdg moving to codidact.com
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
灵感来自支持变音符号的顶帖

