[英]Piecing together a regular expression
I've asked a couple of JavaScript regular expression questions over the last few days as I try to piece together a larger regular expression but I am still having some trouble so I am going to ask about the entire problem, which is probably what I should have done in the first place. 在过去的几天里,当我试图拼凑一个更大的正则表达式时,我问了几个JavaScript正则表达式问题,但是我仍然遇到一些麻烦,因此我将要询问整个问题,这可能是我应该做的首先要做的。
Essentially, what I need is a regular expression that will match all of the following: 本质上,我需要一个匹配以下所有内容的正则表达式:
An empty string. 空字符串。
A string that contains at least one alpha-numeric character but does not start with a +1. 一个字符串,至少包含一个字母数字字符,但不以+1开头。
A string that starts with +1 and has at least 1 more alpha-numeric character. 以+1开头并且至少还有1个字母数字字符的字符串。
So some examples are as follows: 因此,一些示例如下:
"" = true
"+" = false
"+abc" = true
"abc" = true
"+1" = false
"+12" = true
"+2" = true
Based on your stated requirements as amended, you want to match only: 根据修改后的规定要求,您只想匹配:
An empty string, ^$
空字符串, ^$
A string that contains at least one alpha-numeric character but does not start with a +1, ^(?!\\+1).*[a-zA-Z0-9]
一个字符串,至少包含一个字母数字字符,但不以+1 ^(?!\\+1).*[a-zA-Z0-9]
开头^(?!\\+1).*[a-zA-Z0-9]
A string that starts with +1 and has at least 1 more alpha-numeric character, ^\\+1.*[a-zA-Z0-9]
以+1开头并且至少还有1个字母数字字符^\\+1.*[a-zA-Z0-9]
字符串^\\+1.*[a-zA-Z0-9]
Put together, that is: 放在一起,即:
^$|^(?!\+1).*[a-zA-Z0-9]|^\+1.*[a-zA-Z0-9]
Or, if you like: 或者,如果您喜欢:
^($|(?!\+1).*[a-zA-Z0-9]|\+1.*[a-zA-Z0-9])
^(?:\\+1[a-zA-Z0-9]+|(?!\\+1).*[a-zA-Z0-9]+.*)?$
Explanation: 说明:
The regex is separated in two cases: ( CASE1 | CASE2 )
正则表达式在两种情况下分开: ( CASE1 | CASE2 )
First case: \\+1[a-zA-Z0-9]+
matches every text that starts with +1
and is followed by one or more alphanumeric char ( [a-zA-Z0-9]+
stands for pick one or more chars that are either from a to z , from A to Z or from 0 to 9 ) 第一种情况: \\+1[a-zA-Z0-9]+
匹配以+1
开头且后跟一个或多个字母数字字符的每个文本( [a-zA-Z0-9]+
代表选择一个或多个从a到z ,从A到Z或从0到9的字符 )
Second case: (?!\\+1).*[a-zA-Z0-9]+.*
matches every text that does NOT start with +1
( (?!\\+1)
), and is followed by as many characters you want as long as it contains at least one alphanumeric char ( .*[a-zA-Z0-9]+.*
stands for pick 0 or more of whatever char you want, plus the regex explained above, plus 0 or more of whatever char again ) 第二种情况: (?!\\+1).*[a-zA-Z0-9]+.*
匹配不以+1
( (?!\\+1)
)开头的每个文本,后跟尽可能多的文本只要包含至少一个字母数字字符( .*[a-zA-Z0-9]+.*
代表您想要的任何字符,请选择0或多个,加上上面说明的正则表达式,再加上0或多个,则表示您想要的字符无论是什么字符 )
These two cases respectively match your rules #3 and #2 . 这两种情况分别与您的规则#3和#2相匹配。
The rule #1 is taken care of by the ?
规则#1由?
at the end of the whole expression, meaning all of that is optional, therefore it can also be an empty string. 在整个表达式的末尾,意味着所有这些都是可选的,因此它也可以是一个空字符串。
Please note some things such as: 请注意一些事情,例如:
(?:something)
is used to match a string, but not capture it. (?:something)
用于匹配字符串,但不捕获它。 (?!something)
is used to make sure it doesnt match a string (?!something)
用于确保它与字符串不匹配 \\
is used to escape special characters like +
when you want them to stand as regular characters \\
用于转义特殊字符(例如+
当您希望它们作为常规字符站立时 +
is used to say one or more of the preceding item +
用于表示前面的一项或多项 *
is used to say zero or more of the preceding item *
用于表示零个或多个前一项 Hope i helped! 希望我有所帮助!
Based on your most updated requirements: 根据您最新的要求:
Here is what I'd use: 这是我要用的:
/^([]|(\+1)?.*[a-zA-Z0-9]+.*)$/
In plan english, that regex says to look for a string that is: 在计划英语中,该正则表达式表示要查找以下字符串:
+1
as well) 具有字母数字字符(也可以选择以+1
开头) //var re = /(^$)/; //Matches empty
//var re = /(^[a-z0-9]+)/; //matches only string no plus
//var re = /(^\+([0a-z2-9])([a-z0-9].*)?)/; //Matches the + [not one] requirement
//Joined together with | for or
//Might be simplified more, but this works ;)
var re = /(^([a-z0-9]+.*|[a-z0-9]+.*|\+1[a-z0-9]+.*|\+([0a-z2-9])([a-z0-9].*)?)?$)/i;
function testIt( str, expected ) {
if( !!re.exec( str ) === expected ) {
console.info(str + "\tpassed" );
} else{
console.error(str + "\tfailed" );
}
}
testIt("", true);
testIt("+", false);
testIt("+abc", true);
testIt("abc", true);
testIt("+1", false);
testIt("+12", true);
testIt("+12_", true);
testIt("+2", true);
testIt("+2c", true);
testIt("+2_", false);
testIt("+007", true);
A string that contains at least one alpha-numeric character but does not start with a +1.
A string that starts with +1 and has at least 1 more alpha-numeric character.
Let's rephrase this a little bit : 让我们改写一下:
A string that has at least one alpha-numeric character but does not start with a +1. 一个字符串,至少包含一个字母数字字符,但不以+1开头。
A string that starts with +1 and has at least 1 more alpha-numeric character. 以+1开头并且至少还有 1 个字母数字字符的字符串。
Try again : 再试一次 :
but does not start with a +1 | 但不以+1 |开头 A string that has at least one alpha-numeric character A string that starts with +1 | 至少包含一个字母数字字符的字符串以+1 |开头的字符串 and has at least 1 more alpha-numeric character. 并且至少还有 1 个字母数字字符。
So what does this let us too? 那么,这又使我们做什么呢?
Nothing. 没有。 You just wanna match an empty string. 您只想匹配一个空字符串。 I mean really ? 我的意思是真的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.