[英]JS RegEx to match all characters (including newline) between two strings but without the two strings?
<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
$('#put').append(/<div id="wanted">[^<>]*<\/div>/.exec(strg)[0]);
==>
<div id="wanted">I want
only this
text
</div>
如果还有\\n
和\\r
重新发送,Java Script的正则表达式可以在分隔字符串之间传递字符。 如果从传递的字符串中删除\\n
和\\r
,那就太好了。 RegExpr应该可以快速工作。
现在我知道如何:
$('#put').append(/<div id="wanted">([\s\S]*?)<\/div>/.exec(strg)[1]);
谢谢Jerry的(
小组)
提示。 [\\s\\S]
代表每个角色。 *?
在第一次找到<\\/div>
后停止。
您可以使用捕获组并忽略完整匹配?
$('#put').append(/<div id="wanted">([^<>]*)<\/div>/.exec(strg)[1]);
^------^ ^
( ... )
是一个捕获组,因为它是正则表达式中的第一个,它到达第一个捕获组,因此接近结束时的1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.