Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - 如何在多行中使用JavaScript正则表达式?(How to use JavaScript regex over multiple lines?)

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.

(我希望可以拾取PRE块,即使它跨越换行符也是如此。)

I thought the 'm' flag does it.

(我以为'm'标志可以做到。)

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.

(因为我以为我知道JavaScript(读了三本书,花了很多时间),所以SO上没有现有的解决方案,所以无论如何我都敢发表。)

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 [.\n] does not work.

(我仍然不明白是为什么[.\n]不起作用。)

Guess this is one of the sad parts of JavaScript..

(猜猜这是JavaScript的不幸部分之一。)

  ask by akauppi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

DON'T use (.|[\r\n]) instead of .

(不要使用(.|[\r\n])代替.)

for multiline matching.

(用于多行匹配。)

DO use [\s\S] instead of .

(请使用[\s\S]代替.)

for multiline matching

(用于多行匹配)

Also, avoid greediness where not needed by using *?

(另外,在不需要的地方使用*?避免贪婪*?)

or +?

(或+?)

quantifier instead of * or + .

(而不是*+量词。)

This can have a huge performance impact.

(这会对性能产生巨大影响。)

See the benchmark I have made: http://jsperf.com/javascript-multiline-regexp-workarounds

(请参阅我制定的基准测试: http : //jsperf.com/javascript-multiline-regexp-workarounds)

Using [^]: fastest
Using [sS]: 0.83% slower
Using (.|
|
): 96% slower
Using (.|[
]): 96% slower

NB: You can also use [^] but it is deprecated in the below comment.

(注意:您也可以使用[^]但在以下注释中已弃用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...