Javascript正则表达式多行标志不起作用 [英] Javascript regex multiline flag doesn't work

查看:117
本文介绍了Javascript正则表达式多行标志不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个正则表达式来从html中获取字符串,但似乎多行标志不起作用。

I wrote a regex to fetch string from html, but it seems the multiline flag doesn't work.

这是我的模式,我想得到文本在h1标签中。

this is my pattern and I want to get the text in h1 tag.

var pattern= /<div class="box-content-5">.*<h1>([^<]+?)<\/h1>/mi
m = html.search(pattern);
return m[1];

我创建了一个字符串来测试它。当字符串包含\ n时,结果始终为null。如果我删除所有\ n,它会给我正确的结果,无论是否有/ m标志。

I created a string to test it. When the string contains "\n" the result is always null. If I remove all the "\n" , it gave me the right result, no matter with or without /m flag.

我的正则表达式有什么问题?

what's wrong with my regex?

推荐答案

您正在寻找 /.../ 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 ),像这样:

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

这篇关于Javascript正则表达式多行标志不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆