我想使用此代码从文档中删除除<a>
<img>
和所有的HTML标签:删除HTML标签
var regex = "<(?!a)(?!img)(?!iframe)([\s\S]*?)>";
var temp;
while (source.match(regex)) {
temp = source.match(regex)[0];
source = source.replace(temp, "");
}
return source;
它可以在网上正则表达式测试仪,但由于某些原因,它不在我的网页上工作。例如,当输入为时,它会返回原始字符串:
"<p class="MsoNormal" style="margin-left:202.5pt;line-height:200%;background:white"><b><span style="font-size: 16pt; line-height: 200%; color: rgb(131, 60, 11); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">test</span></b><span style="font-size:16.0pt;
line-height:200%;color:#833C0B;letter-spacing:-.15pt;mso-ansi-language:EN-US"><o:p></o:p></span></p>"
请帮忙!
你能确切说出你想干什么? –
是不是'[\ s \ S]'等同于'.'?你有没有尝试在你的'while'循环中添加'console.log(temp)'(或者设置一个中断点)来查看实际发生的事情? – nnnnnn
[\ s \ S]允许正则表达式匹配多行 – levkaster