[英]Regular Expression/JS – remove content from specific xml tags and keep only text
I'd like to remove content from specific tags and keep only the text.我想从特定标签中删除内容并只保留文本。 I'm using Javascript but on React Native, so I can't use Dom or JQuery.
我正在使用 Javascript 但在 React Native 上,所以我不能使用 Dom 或 JQuery。
For example, for this input, I'd like to remove all the sub-tags of "li":例如,对于这个输入,我想删除“li”的所有子标签:
<div>
<div><span>hello</span></div>
<ul>
<li><div><span>hello 2</span></div></li>
<li><div><span>hello 3</span></div></li>
</ul>
</div>
I'd like to get this output:我想要这个 output:
<div>
<div><span>hello</span></div>
<ul>
<li>hello 2</li>
<li>hello 3</li>
</ul>
</div>
How can I achieve this?我怎样才能做到这一点? (again, not using DOM or JQuery)
(同样,不使用 DOM 或 JQuery)
Thank you!谢谢!
Here is a native JavaScript way to filter out specific tags within <li>
tags:这是一个原生的 JavaScript 方法来过滤掉
<li>
标签中的特定标签:
var str = '<div>\n' + ' <div><span>hello</span></div>\n' + ' <ul>\n' + ' <li><div><span>hello 2</span></div></li>\n' + ' <li><div><span>hello 3</span></div></li>\n' + ' </ul>\n' + '</div>'; var re1 = /(<li> *)(.*?)(<\/li>)/g; var re2 = /<\/?(div|span)\b[^>]*> */g; var result = str.replace(re1, function(m, p1, p2, p3) { return p1 + p2.replace(re2, '') + p3; }); console.log('result:\n' + result);
Console output:控制台 output:
result:
<div>
<div><span>hello</span></div>
<ul>
<li>hello 2</li>
<li>hello 3</li>
</ul>
</div>
Explanation:解释:
re1
identifies <li>
tags re1
标识<li>
标签
g
flag to match multiple timesg
标志p1
, p2
, p3
p1
, p2
, p3
div
and span
tags from p2
, which is the content within the <li>
tagp2
中删除所有div
和span
标签,这是<li>
标签中的内容
re2
to add additional tags to filterre2
以添加额外的标签来过滤g
flag to match multiple timesg
标志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.