【发布时间】:2022-01-11 06:36:42
【问题描述】:
尝试匹配包含某些单词的文档的整个句子,即使该句子跨越多行。
我当前的尝试只捕获不跨越下一行的句子。
^.*\b(dog|cat|bird)\b.*\.
使用 ECMAScript。
【问题讨论】:
标签: javascript regex
尝试匹配包含某些单词的文档的整个句子,即使该句子跨越多行。
我当前的尝试只捕获不跨越下一行的句子。
^.*\b(dog|cat|bird)\b.*\.
使用 ECMAScript。
【问题讨论】:
标签: javascript regex
当输入中没有缩写时使用
/\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: '.', '?', '!'
【讨论】: