【问题标题】:Regular Expression square brackets with asterisk带星号的正则表达式方括号
【发布时间】:2016-08-11 06:36:28
【问题描述】:

我有以下文字:

敏捷的棕色狐狸跳过了懒惰的狗。 FCC 不得不审查网络说 &$#*@!。有614名学生获得90.0%或以上。

我想找到文字:

&$#*@!

我定义了以下正则表达式来查找上述文本:

[^0-9a-zA-Z\s.]

它确实从上面的文本中找到了匹配项,但我想找到重复出现的地方。如果我只是多次输入它,它确实有效。像这样:

[^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.][^0-9a-zA-Z\s.]

现在我用星号表示零次或多次出现:

[^0-9a-zA-Z\s.]*
([^0-9a-zA-Z\s.])*

他们不工作。但是,当我尝试这个时(使用/g 修饰符):

([^0-9a-zA-Z\s.]*)

我得到了大约 155 个结果。见此链接:https://regex101.com/r/yJ9dN7/2

如何修改上述代码以匹配&$#*@!

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    使用 + 量词匹配 1 次或多次出现:

    [^0-9a-zA-Z\s.]+
                   ^
    

    regex demo

    或者,如Sebastian Proske comments,为了使匹配更精确,您可以使用限制量词 {2,} 匹配2 个或更多,或{3,} 匹配3 个或更多,等等。 (请参阅底部的备忘单)。

    var re = /[^0-9a-zA-Z\s.]+/; 
    var str = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
    if (m=str.match(re)) {
      console.log(m[0]);
    }

    Quantifier Basics

    JS 量词备忘单

    +          once or more
      A+       One or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
      A+?      One or more As, as few as needed to allow the overall pattern to match (lazy)
    *      zero times or more
      A*       Zero or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
      A*?      Zero or more As, as few as needed to allow the overall pattern to match (lazy)
    ?        zero times or once
      A?       Zero or one A, one if possible (greedy), giving up the character if the engine needs to backtrack (docile)
      A??      Zero or one A, zero if that still allows the overall pattern to match (lazy)
    {x,y}     x times at least, y times at most
      A{2,9}    Two to nine As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
      A{2,9}?   Two to nine As, as few as needed to allow the overall pattern to match (lazy)
      A{2,}   Two or more As, greedy
      A{2,}?    Two or more As, lazy (non-greedy).
      A{5}  Exactly five As. Fixed repetition: neither greedy nor lazy.
    

    【讨论】:

    • 我更喜欢{2,} 而不是+,因为如果它在&$#*@! 之前,您的% 也会匹配90.0% 的登录
    • @SebastianProske:选择权取决于 OP,我添加了一个完整的量词备忘单。
    【解决方案2】:

    如果你只想得到 &$#*@!然后使用这个正则表达式,

    regex=(&\$#\*@!)
    

    你可以在下面link看到结果。

    【讨论】:

      【解决方案3】:
      var text = 'The quick brown fox jumped over the lazy dog. The FCC had to censor the network for saying &$#*@!. There were 614 instances of students getting 90.0% or above.';
      
      console.log(text.match(/[^A-z0-9\s.]+/g, ''));
      

      这种情况下的问题是“90.0%”的%,你想要这个字符吗?或者你可以排除它?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多