简体   繁体   中英

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.

Essentially, what I need is a regular expression that will match all of the following:

  1. An empty string.

  2. A string that contains at least one alpha-numeric character but does not start with a +1.

  3. A string that starts with +1 and has at least 1 more alpha-numeric character.

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]

  • A string that starts with +1 and has at least 1 more alpha-numeric character, ^\\+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 )

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 )

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 )

These two cases respectively match your rules #3 and #2 .

The rule #1 is taken care of by the ? 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) is used to make sure it doesnt match a string
  • \\ 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:

  1. An empty string.
  2. A string that contains at least one alpha-numeric character but does not start with a +1.
  3. A string that starts with +1 and has at least 1 more alpha-numeric character.

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. Empty, or
  2. Has an alphanumberic character (and optionally starts with +1 as well)
//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);

JSFiddle

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.
A string that starts with +1 and has at least 1 more alpha-numeric character.

Try again :

but does not start with a +1 | A string that has at least one alpha-numeric character A string that starts with +1 | and has at least 1 more alpha-numeric character.

So what does this let us too?

Nothing. You just wanna match an empty string. I mean really ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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