简体   繁体   English

带正则表达式的String.match()

[英]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. 字符串中的每个字母都必须用“ +”符号括起来才能通过,在这种情况下,验证函数应返回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). 为此,我使用带有正则表达式的String.match()来识别任何非法模式,如果匹配返回空值(即未找到非法模式),则返回true。

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. 当我在正则表达式测试器上测试它时,我的正则表达式似乎可以正常工作,但是,在类似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 +) 您的正则表达式搜索(anything other than +)(letter)(+)(+)(letter)(anything other than +)

+a+a will be matched because end of the string is (anything other than +) and +a is a match. +a+a将被匹配,因为字符串的结尾是(anything other than +)字符(anything other than +)+a是匹配项。

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. 上面允许的字符是alpha和加号。
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. 我在您的RegEx中添加了更多案例并被唤醒。 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

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

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