【问题标题】:Matching an entire sentence containing words even if the sentence spans multiple lines匹配包含单词的整个句子,即使句子跨越多行
【发布时间】:2022-01-11 06:36:42
【问题描述】:

尝试匹配包含某些单词的文档的整个句子,即使该句子跨越多行。

我当前的尝试只捕获不跨越下一行的句子。

^.*\b(dog|cat|bird)\b.*\.

使用 ECMAScript。

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    当输入中没有缩写时使用

    /\b[^?!.]*?\b(dog|cat|bird)\b[^?!.]*[.?!]/gi
    

    regex proof

    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      [^?!.]*?                 any character except: '?', '!', '.' (0 or
                               more times (matching the least amount
                               possible))
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        dog                      'dog'
    --------------------------------------------------------------------------------
       |                        OR
    --------------------------------------------------------------------------------
        cat                      'cat'
    --------------------------------------------------------------------------------
       |                        OR
    --------------------------------------------------------------------------------
        bird                     'bird'
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      [^?!.]*                  any character except: '?', '!', '.' (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      [.?!]                    any character of: '.', '?', '!'
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多