且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript正则表达式多行标志不起作用

更新时间:2023-02-26 11:34:04

您正在寻找 /.../ s 修饰符,称为 dotall 修饰符。它强制点也匹配新行,默认情况下它不执行

You are looking for the /.../s modifier, also known as the dotall modifier. It forces the dot . to also match newlines, which it does not do by default.

坏消息是JavaScript 中不存在(从ES2018开始,见下文)。好消息是你可以通过使用字符类(例如 \s )及其否定( \S $)来解决它。 c $ c>),像这样:

The bad news is that it does not exist in JavaScript (it does as of ES2018, see below). The good news is that you can work around it by using a character class (e.g. \s) and its negation (\S) together, like this:

[\s\S]

因此,在您的情况下,正则表达式将成为:

So in your case the regex would become:

/<div class="box-content-5">[\s\S]*<h1>([^<]+?)<\/h1>/i






从ES2018开始,JavaScript支持 s (dotAll)标志,所以在现代环境中你的正则表达式可能就像你写的一样,但是带有 s 标志结束(而不是 m ; m 改变 ^ 的方式和 $ 工作,而不是):


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 .):

/<div class="box-content-5">.*<h1>([^<]+?)<\/h1>/is