嘗試匹配包含某些單詞的檔案的整個句子,即使該句子跨越多行。
我目前的嘗試只捕獲沒有跨越下一行的句子。
^.*\b(dog|cat|bird)\b.*\.
使用 ECMAScript。
uj5u.com熱心網友回復:
當輸入中沒有縮寫時應使用
[^?!.\s][^?!.]*?\b(dog|cat|bird)\b[^?!.]*[.?!]
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
[^?!.\s] any character except: '?', '!', '.',
whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
[^?!.]*? 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: '.', '?', '!'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373314.html
標籤:javascript 正则表达式
