简体   繁体   中英

String.match() with regular expression

I am having trouble with some code I am using to validate a string. Every letter in the string must be surrounded by a '+' symbol to pass, in which case the validation function should return true. To do this, I am using String.match() with a regex to identify any illegal patterns, and return true if the match returns a null value (ie no illegal patterns are found).

My regular expression seems to work when I test it on a regex tester however, the match fails when testing it on something like jsbin. Can anyone tell me what I am doing wrong, and/or whether there is a better way I should be testing this?

/* Using the JavaScript language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable        sequence by either returning the string true or false. The str parameter  will be composed of + and = symbols with several letters between them (ie.++d+===+c++==a) and for the string to be true each letter must be     surrounded by a + symbol. So the string to the left would be false. The     string will not be empty and will have at least one letter. 
*/

function SimpleSymbols(str){
  // search for illegal patterns (i.e. ^+[a-z]+ or +[a-z]^+)
  var illegalPatterns = str.match( /[^+][a-z][+]|[+][a-z][^+]/ig );
  // return true if no illegal patterns are found
    return illegalPatterns === null;
}

console.log(SimpleSymbols("++a+d+"));    // expect true       
console.log(SimpleSymbols("+a+a"));  // expect false
console.log(SimpleSymbols("+a++++a+"));  // expect true
console.log(SimpleSymbols("+a++++aa+"));  // expect false

Can anyone tell me what I am doing wrong

Your regex searches for (anything other than +)(letter)(+) or (+)(letter)(anything other than +)

+a+a will be matched because end of the string is (anything other than +) and +a is a match.

You can use the following instead:

[a-z]{2}|^[a-z]|[a-z]$          //check if two characters are not 
                                //separated by `+` and return false

If this matches, the test passed

 # ^\++(?:[a-zA-Z]\++)+$

 ^ 
 \++
 (?: [a-zA-Z] \++ )+
 $

edit
The above allowed characters are alpha and plus.
So, it appears you want to have any characters, but only the alpha's must be surrounded by plus.

That is much more complicated.
That situation is handled by the below regex.
When this matches, NO flaw is found.

 # ^(?:[^a-zA-Z+]+|(?:(?:\++[a-zA-Z](?=\+))*|\++))*$

 ^                    # BOS
 (?:                  # 0 to many of either non-apha's or alphas surrounded by plus
      [^a-zA-Z+]+          # 1 to many, Not alpha nor plus
   |                     # or,
      (?:
           (?:                  # 0 to many, plus alpha
                \++
                [a-zA-Z]             # Single alpha
                (?= \+ )             # Assert, plus ahead
           )*
        |                     # or,
           \++                  # 1 to many plus
      )
 )*
 $                    # EOS

edit
And I guess you could do the previous regex like this..
But, when this matches, a flaw WAS found.

 # (?:^|[^+])[a-zA-Z]|[a-zA-Z](?!\+)

    (?: ^ | [^+] )
    [a-zA-Z] 
 |  
    [a-zA-Z] 
    (?! \+ )

Try it:

^[a-z][+]|[^+][a-z][+]|[+][a-z][^+]|[+][a-z]$

I added more cases to your RegEx and woked. I am testing boundary cases too

DEMO

 function SimpleSymbols(str){ var illegalPatterns = str.match(/^[az][+]|[^+][az][+]|[+][az][^+]|[+][az]$/ig); // return true if no illegal patterns are found return illegalPatterns === null; } var result = "" result += SimpleSymbols("++a+d+") + "\\n"; // expect true result += SimpleSymbols("+a+a") + "\\n"; // expect false result += SimpleSymbols("+a++++a+") + "\\n"; // expect true result += SimpleSymbols("+a++++aa+") + "\\n"; // expect false document.getElementById("results").innerHTML = result; 
 <div id="results"></div> 


But I prefer to test the valid ones like:

(?:\++[a-zA-Z]\++)+[a-zA-Z]\++

You can use this regex for matching your valid string:

/^\+*(?:\++[a-zA-Z](?=\+))*\+*$/gmi

RegEx Demo


EDIT: To detect invalid matches you can also use:

/[^+][a-z](?=\+)|\+[a-z](?!\+)/gi

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