[英]regex exclude certain tag
I'm cleaning the output created by a wysiwyg, where instead of inserting a break it simply creates an empty p tag, but it sometimes creates other empty tags that's not needed. 我正在清理所见即所得的输出,而不是插入中断,它只是创建一个空的p标签,但有时会创建其他不需要的空标签。
I have a regex to remove all empty tags, but I want to exclude empty p tags from it. 我有一个正则表达式删除所有空标签,但我想从中排除空p标签。 how do I do that?
我怎么做?
let s = "<h1>test</h1><h1></h1><p>a</p><p></p><h2></h2>"; s = s.trim().replace( /<(\\w*)\\s*[^\\/>]*>\\s*<\\/\\1>/g, '' ) console.log(s)
Add (?!p)
to your regex. 将
(?!p)
添加到您的正则表达式中。 This is called Negative Lookahead
: 这称为
Negative Lookahead
:
let s = "<h1>test</h1><h1></h1><p>a</p><p></p><h2></h2>"; s = s.trim().replace( /<(?!p)(\\w*)\\s*[^\\/>]*>\\s*<\\/\\1>/g, '' ) console.log(s)
I understand that you want to use regex for that, but there are better ways. 我知道您想为此使用正则表达式,但是有更好的方法。 Consider using
DOMParser
: 考虑使用
DOMParser
:
var x = "<h1>test</h1><h1></h1><p>a</p><p></p><h2></h2>"
var parse = new DOMParser;
var doc = parse.parseFromString(x,"text/html");
Array.from(doc.body.querySelectorAll("*"))
.filter((d)=>!d.hasChildNodes() && d.tagName.toUpperCase() !== "P")
.forEach((d)=>d.parentNode.removeChild(d));
console.log(doc.body.innerHTML);
//"<h1>test</h1><p>a</p><p></p>"
You can wrap the above in a function and modify as you like. 您可以将以上内容包装在函数中,并根据需要进行修改。
You can use DOMParser
to be on the safe side. 为了安全起见,可以使用
DOMParser
。
let s = "<h1>test</h1><h1></h1><p>a</p><p></p><h2></h2>"; const parser = new DOMParser(); const doc = parser.parseFromString(s, 'text/html'); const elems = doc.body.querySelectorAll('*'); [...elems].forEach(el => { if (el.textContent === '' && el.tagName !== 'P') { el.remove(); } }); console.log(doc.body.innerHTML);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.