JS RegEx可以匹配两个字符串之间的所有字符(包括换行符),但不包含两个字符串? [英] JS RegEx to match all characters (including newline) between two strings but without the two strings?

查看:734
本文介绍了JS RegEx可以匹配两个字符串之间的所有字符(包括换行符),但不包含两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div id="not-wanted">
no no 
no 
</div>
<div id="wanted">I want 
only this 
text
</div> no no no
no no
<div id="not-wanted">
no no no 
</div>
<div id="wanted">no no
no no</div>
<div id="wanted">
no no     
</div>

应交付:

I want 
only this 
text

或更佳:

I want only this text

不幸的是,我的解决方案还捕获了2个定界字符串:

$('#put').append(/<div id="wanted">[^<>]*<\/div>/.exec(strg)[0]);

==>

<div id="wanted">I want 
only this 
text
</div>

在线示例

http://regex101.com/r/rF7jR9

如果还有\n\r重发,则Java Script的正则表达式可以在定界字符串之间传递字符.如果将\n\r从提供的字符串中删除,那将很好. RegExpr应该可以快速运行.

What regular expression for Java Script can deliver the characters between delimiting strings, if there are also \n and \r resend. It would be nice, if \n and \r are removed from the delivered string. The RegExpr should work fast.

推荐答案

现在我知道如何:

$('#put').append(/<div id="wanted">([\s\S]*?)<\/div>/.exec(strg)[1]);

感谢Jerry提供的( group )提示. [\s\S]代表每个字符. *?在第一次找到<\/div>之后停止.

Thank you Jerry for the (group) hint. [\s\S] stands for every character. *? stop after first found <\/div>.

这篇关于JS RegEx可以匹配两个字符串之间的所有字符(包括换行符),但不包含两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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