如何在多行上使用 JavaScript 正则表达式? [英] How to use JavaScript regex over multiple lines?

查看:34
本文介绍了如何在多行上使用 JavaScript 正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var ss="

aaaa
bbb
ccc

ddd";var arr= ss.match(/

/gm );警报(arr);//空值

我希望 PRE 块被拾取,即使它跨越换行符.我认为 'm' 标志可以做到.没有.

在发布之前此处找到了答案.由于我认为我了解 JavaScript(阅读三本书,工作时间)并且在 SO 上没有现有的解决方案,所以无论如何我都敢发帖.在这里扔石头

所以解决方案是:

var ss="

aaaa
bbb
ccc

ddd";var arr= ss.match(/

/gm );警报(arr);//<pre>...</pre>:)

有没有人有一个不那么神秘的方法?

this 是重复的,但由于它比我的更难找到,我不删除.

它建议将 [^] 作为多行点".我仍然不明白为什么 [. ] 不起作用.猜猜这是 JavaScript 的可悲部分之一..

解决方案

[. ] 不起作用,因为 . 内部没有特殊含义[],它只是表示文字 ..(.| ) 将是一种指定任何字符,包括换行符"的方法.如果要匹配所有换行符,还需要添加 以包含 Windows 和经典 Mac OS 样式的行结尾:(.|[ ]).

结果证明这有点麻烦,而且速度很慢(请参阅 KrisWebDev 的详细回答),所以更好的方法是匹配所有空白字符和所有非空白字符,使用 [sS],它会匹配所有内容,并且更快更简单.

一般来说,您不应该尝试使用正则表达式来匹配实际的 HTML 标签.例如,请参见 这些 问题 了解更多信息.

相反,尝试在 DOM 中实际搜索您需要的标签(使用 jQuery 使这更容易,但您始终可以使用标准 DOM 执行 document.getElementsByTagName("pre")),然后如果需要匹配内容,请使用正则表达式搜索这些结果的文本内容.

var ss= "<pre>aaaa
bbb
ccc</pre>ddd";
var arr= ss.match( /<pre.*?</pre>/gm );
alert(arr);     // null

I'd want the PRE block be picked up, even though it spans over newline characters. I thought the 'm' flag does it. Does not.

Found the answer here before posting. SInce I thought I knew JavaScript (read three books, worked hours) and there wasn't an existing solution at SO, I'll dare to post anyways. throw stones here

So the solution is:

var ss= "<pre>aaaa
bbb
ccc</pre>ddd";
var arr= ss.match( /<pre[sS]*?</pre>/gm );
alert(arr);     // <pre>...</pre> :)

Does anyone have a less cryptic way?

Edit: this is a duplicate but since it's harder to find than mine, I don't remove.

It proposes [^] as a "multiline dot". What I still don't understand is why [. ] does not work. Guess this is one of the sad parts of JavaScript..

解决方案

[. ] does not work because . has no special meaning inside of [], it just means a literal .. (.| ) would be a way to specify "any character, including a newline". If you want to match all newlines, you would need to add as well to include Windows and classic Mac OS style line endings: (.|[ ]).

That turns out to be somewhat cumbersome, as well as slow, (see KrisWebDev's answer for details), so a better approach would be to match all whitespace characters and all non-whitespace characters, with [sS], which will match everything, and is faster and simpler.

In general, you shouldn't try to use a regexp to match the actual HTML tags. See, for instance, these questions for more information on why.

Instead, try actually searching the DOM for the tag you need (using jQuery makes this easier, but you can always do document.getElementsByTagName("pre") with the standard DOM), and then search the text content of those results with a regexp if you need to match against the contents.

这篇关于如何在多行上使用 JavaScript 正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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