匹配包含单词的整个句子,即使该句子跨越多行

  • 本文关键字:句子 跨越 包含单 javascript regex
  • 更新时间 :
  • 英文 :


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

我目前的尝试只有在句子没有延伸到下一行的情况下才能捕捉到它。

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

使用ECMAScript。

当输入中没有缩写时,使用

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

请参阅正则表达式证明。

解释

--------------------------------------------------------------------------------
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: '.', '?', '!'

最新更新