javascript 匹配任何数字的正则表达式模式包括 1-9 除了 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16999328/
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
Regex pattern that matches any number include 1-9 except 2
提问by Mohammad Masoudian
I need a regular expression pattern that matches any number including 1-9 numbers except 2?
我需要一个匹配任何数字的正则表达式模式,包括 1-9 数字,除了 2?
My attempt:
我的尝试:
([1-9][^2])
But this doesn't work for me.
但这对我不起作用。
回答by Stuart Wakefield
Another way to do it:
另一种方法:
/[^\D2]/
Which means, not a non-digit or 2.
这意味着,不是非数字或 2。
回答by Lepidosteus
You can match the range of numbers before and after two with [0-13-9]
, like this:
您可以将两个前后的数字范围与 匹配[0-13-9]
,如下所示:
"4526".match(/[0-13-9]+/)
["45"]
"029".match(/[0-13-9]+/)
["0"]
"09218".match(/[0-13-9]+/)
["09"]
回答by nab1x9
Or this is also the correct answer.
或者这也是正确答案。
/(?!2)\d/
/(?!2)\d/
回答by Prakash GPz
This RegExp works: /([013-9])/
这个正则表达式有效: /([013-9])/