简体   繁体   English

正则表达式验证多个条件

[英]Regular Expression to validate multiple condition

I require to validate a string using javascript with multiple conditions. 我需要使用具有多个条件的javascript验证字符串。 I am not sure how to do this using regular expressions. 我不知道如何使用正则表达式来做到这一点。

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). 条件1 :以18XX或1-8XX或8XX开头的任何长度的字符串,其中X是0到9之间的任何数字(0和9都包括在内)。 Ex: 1800abc, 812abc-def, 1-805-999-9999 例如: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). 条件2 :以NXX或1NXX或1-NXX开头的字符串后跟正好七个不包括连字符的数字,其中N是2到9之间的任意数字(包括2和9),X是0到9之间的任意数字( 0和9都包括在内)。 Ex: 12-999-9999, 19009998888, 1-212---1-2-3-4-5-6-7-- 例如: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). 条件3 :以XXXXX开头的字符串,其中X是0到9之间的任何数字(0和9都包括在内)。 Ex: 20176, 90210-Melrose 例:20176,90210-Melrose

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 not match any. 尝试将这三个用于每个条件,检查所有三个条件并仅传递那些匹配的条件。

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

condition 2: remove all hypens first then 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 @nnnnn in the comments, lets first check if the string actually begins with a - if it does then the string passes validation without further checks. 因为-似乎是一个可选字符,我们先删除它们,但正如@nnnnn在注释中指出的那样,首先要检查字符串是否实际上 -如果是,那么字符串通过验证而不进一步检查。 then you can string those three together with | 那么你可以串这三个连同| form a single regex which you can check against 形成一个你可以检查的正则表达式

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

i removed -? 我删除了-? from the first part since we've already removed all hypens. 从第一部分开始,因为我们已经删除了所有的大肆宣传。

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 \\d or just plain [0-9] . 要匹配数字,您可以使用数字class \\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 groups to come up with something like: 当然,您可以通过可选项目来获得智能,例如:

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

You can use a tool like RegexPal to experiment with regular expressions. 您可以使用RegexPal等工具来试验正则表达式。 I usually just play with it in the Chrome Developer Tools console. 我通常只是在Chrome开发者工具控制台中使用它。

Try to figure out the rest on your own. 试着自己弄清楚其余部分。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM