[英]How to select this HTML codes using Regular Expression?
我有几行 HTML 代码:
<div class="container header"> <div class="row"> <div class="col-md-6">This is the target text</div> </div> </div>
如何使用正则表达式选择<div class="container header">
?
最终结果应该是:
<div class="row"> <div class="col-md-6">This is the target text</div> </div>
这个怎么样?
<div class="row">[ \n\s]*<div class="col-md-6">[^<]*<\/div>[ \n\s]*<\/div>
[\\n\\s]*
确保您只阅读新空间或空间[^<]*
读取除<
之外的所有内容,这是您停止的字符<\\/div>[ \\n\\s]*<\\/div>
读取您请求的两个结束 div 标签当然,您必须使用s
选项运行它,以便点与换行符匹配。
DOMParser
可用于解析 html:
var html = ` <div class="container header"> <div class="row"> <div class="col-md-6">This is the target text</div> </div> </div>` var innerHTML = new DOMParser().parseFromString(html, 'text/xml').firstChild.innerHTML console.log( innerHTML )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.