gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:07:31 24 4
gpt4 key购买 nike

尝试匹配包含特定单词的文档的整个句子,即使该句子跨越多行也是如此。

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

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

使用 ECMAScript。

最佳答案

当输入中没有缩写需要使用时

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

关于javascript - 匹配包含单词的整个句子,即使句子跨越多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70238940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com