示例文本:
<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>
在线示例
问题
Java Script 的正则表达式可以传递分隔字符串之间的字符,如果还有\n
和\r
重新发送。如果从传递的字符串中删除 \n
和 \r
就好了。 RegExpr 应该运行得很快。
最佳答案
现在我知道如何:
$('#put').append(/<div id="wanted">([\s\S]*?)<\/div>/.exec(strg)[1]);
谢谢 Jerry (
集团)
暗示。 [\s\S]
代表每一个 Angular 色。 *?
第一次找到后停止 <\/div>
.
关于javascript - JS RegEx匹配两个字符串之间但没有两个字符串的所有字符(包括换行符)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531252/