You are looking for the /.../s
modifier, also known as the dotall modifier.(您正在寻找/.../s
修饰符,也称为dotall修饰符。)
It forces the dot .
(它强迫点.
) to also match newlines, which it does not do by default.(也匹配换行符,默认情况下不会这样做 。)
The bad news is that it does not exist in JavaScript (it does as of ES2018, see below) .(坏消息是它在JavaScript中不存在 (从ES2018开始,见下文) 。) The good news is that you can work around it by using a character class (eg \s
) and its negation ( \S
) together, like this:(好消息是你可以通过使用一个字符类(例如\s
)和它的否定( \S
)来解决它,如下所示:)
[sS]
So in your case the regex would become:(所以在你的情况下,正则表达式将成为:)
/<div class="box-content-5">[sS]*<h1>([^<]+?)</h1>/i
As of ES2018, JavaScript supports the s
(dotAll) flag, so in a modern environment your regular expression could be as you wrote it, but with an s
flag at the end (rather than m
; m
changes how ^
and $
work, not .
):(从ES2018开始,JavaScript支持s
(dotAll)标志,所以在现代环境中你的正则表达式可能就像你写的一样,但最后有一个s
标志(而不是m
; m
改变^
和$
工作方式,而不是.
):)
/<div class="box-content-5">.*<h1>([^<]+?)</h1>/is
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…