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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:43:29  来源:igfitidea点击:

Regex pattern that matches any number include 1-9 except 2

javascriptjqueryregex

提问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])/