JavaScript 正则表达式:替换所有HTML标签
以下是可以替换所有HTML标记的正则表达式:
/<[^>]+>/g
这个正则表达式会匹配所有的HTML标记,包括标签名和属性,如<a href='http://example.com'>link</a>会被替换为link。如果需要保留一些标记,可以使用replace方法的回调函数。例如,以下代码会保留<a>标签和 :
const html = '<a href='http://example.com'>link</a> ';
const result = html.replace(/<[^>]+>/g, (match) => {
if (match === '<a>' || match === ' ') {
return match;
} else {
return '';
}
});
console.log(result); // <a>link</a>
原文地址: https://www.cveoy.top/t/topic/lR8k 著作权归作者所有。请勿转载和采集!