繁体   English   中英

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

[英]String.match() with regular expression

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

当我在正则表达式测试器上测试它时,我的正则表达式似乎可以正常工作,但是,在类似jsbin的测试中,匹配失败。 谁能告诉我我做错了什么,和/或是否有更好的方法应该对此进行测试?

/* 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

谁能告诉我我在做什么错

您的正则表达式搜索(anything other than +)(letter)(+)(+)(letter)(anything other than +)

+a+a将被匹配,因为字符串的结尾是(anything other than +)字符(anything other than +)+a是匹配项。

您可以改用以下内容:

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

如果匹配,则测试通过

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

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

编辑
上面允许的字符是alpha和加号。
因此,您似乎想要包含任何字符,但只有字母必须被加号包围。

那要复杂得多。
这种情况由下面的正则表达式处理。
当匹配时, 没有发现缺陷。

 # ^(?:[^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

编辑
我猜你可以像这样做以前的正则表达式。
但是,当这个比赛,一个漏洞发现。

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

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

试试吧:

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

我在您的RegEx中添加了更多案例并被唤醒。 我也在测试边界案例

演示

 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> 


但是我更喜欢测试有效的方法,例如:

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

您可以使用此正则表达式匹配您的有效字符串:

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

正则演示


编辑:要检测无效匹配,您还可以使用:

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

暂无
暂无

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

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