javascript 正则表达式来验证多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13024122/
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-26 17:39:56  来源:igfitidea点击:

Regular Expression to validate multiple condition

javascriptregexvalidation

提问by Abishek

I require to validate a string using javascript with multiple conditions. I am not sure how to do this using regular expressions.

我需要使用具有多个条件的 javascript 验证字符串。我不确定如何使用正则表达式来做到这一点。

I need to check if the string does not contain any of the following conditions

我需要检查字符串是否不包含以下任何条件

Condition 1: String of any length beginning with 18XX or 1-8XX or 8XX, where X is any number from 0 to 9 (both 0 and 9 being inclusive). Ex: 1800abc, 812abc-def, 1-805-999-9999

条件 1:以 18XX 或 1-8XX 或 8XX 开头的任意长度的字符串,其中 X 是 0 到 9 之间的任意数字(包括 0 和 9)。例如:1800abc、812abc-def、1-805-999-9999

Condition 2: The string beginning with NXX or 1NXX or 1-NXX followed by exactly seven numbers not including hyphens, where N is any number from 2 to 9 (both 2 and 9 being inclusive) and X is any number from 0 to 9 (both 0 and 9 being inclusive). Ex: 12-999-9999, 19009998888, 1-212---1-2-3-4-5-6-7--

条件 2:以 NXX 或 1NXX 或 1-NXX 开头的字符串后跟不包括连字符的 7 个数字,其中 N 是 2 到 9 之间的任何数字(包括 2 和 9),X 是 0 到 9 之间的任何数字( 0 和 9 都包含在内)。例如:12-999-9999、19009998888、1-212---1-2-3-4-5-6-7--

Condition 3: The string beginning with XXXXX, where X is any number from 0 to 9 (both 0 and 9 being inclusive). Ex: 20176, 90210-Melrose

条件 3:以 XXXXX 开头的字符串,其中 X 是 0 到 9 之间的任意数字(包括 0 和 9)。例如:20176、90210-梅尔罗斯

回答by mzzzzb

you cannot have a single regex match all this,Er may be you can!! see below

你不能有一个正则表达式匹配所有这些,呃你可能可以!!见下文

try using these three for each condition, check for all three and pass only those that do notmatch any.

尝试对每个条件使用这三个,检查所有三个并只通过那些匹配的那些。

condition 1: ^1?-?8\d{2}.*$

条件 1: ^1?-?8\d{2}.*$

condition 2: remove all hypens firstthen match for ^1?[2-9]\d{7}$

条件 2:首先删除所有连字符然后匹配^1?[2-9]\d{7}$

condition 3: ^\d{5}.*$

条件 3: ^\d{5}.*$

hope this helps

希望这可以帮助

EDIT

编辑

You may have a single regex that can match this. since -seems to be an optional character lets remove them first, but as pointed out by @nnnnnin the comments, lets first check if the string actually beginswith a -if it does then the string passes validation without further checks. then you can string those three together with |form a single regex which you can check against

您可能有一个可以匹配的正则表达式。因为-似乎是一个可选字符,所以让我们先删除它们,但正如@nnnnn在评论中指出的那样,让我们首先检查字符串是否实际上以 a开头-如果它是,则字符串通过验证而无需进一步检查。然后你可以将这三个串在一起|形成一个你可以检查的正则表达式

^1?8\d{2}.*|1?[2-9]\d{7}|\d{5}.*

^1?8\d{2}.*|1?[2-9]\d{7}|\d{5}.*

i removed -?from the first part since we've already removed all hypens.

-?从第一部分中删除了,因为我们已经删除了所有的连字符。

回答by sirhc

You're probably looking for alternation: http://www.regular-expressions.info/alternation.html

你可能正在寻找替代:http: //www.regular-expressions.info/alternation.html

To match digits, you can use the digits class \dor just plain [0-9].

要匹配数字,您可以使用digits 类\d或只使用plain [0-9]

For example for condition 1, you can attempt to match it with:

例如对于条件 1,您可以尝试将其与:

 /^18\d\d|1-8\d\d|8\d\d$/.test("1800")  == true
 /^18\d\d|1-8\d\d|8\d\d$/.test("1-800") == true
 /^18\d\d|1-8\d\d|8\d\d$/.test("812")   == true

Of course, you can get smart with optional items, and groupsto come up with something like:

当然,您可以使用optional items来聪明提出以下内容:

 /^(1-?)?8\d\d$/.test("1-800") == true

You can use a tool like RegexPalto experiment with regular expressions. I usually just play with it in the Chrome Developer Tools console.

您可以使用RegexPal 之类的工具来试验正则表达式。我通常只是在 Chrome 开发者工具控制台中使用它。

Try to figure out the rest on your own. :)

其余的尝试自己解决。:)