如何从JavaScript中的字符串中删除html标签?
从字符串中删除HTML标签
我们可以使用javascript中的正则表达式删除字符串中的HTML/XML标签。HTML元素(例如span,div等)出现在左右箭头之间,例如<div>,<span>等。因此,用nothing('')替换箭头中的内容以及箭头,可以完成我们的任务简单。
语法
str.replace( /(<([^>]+)>)/ig, '');
示例1
<html>
<body>
<script>
function removeTags(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace( /(<([^>]+)>)/ig, '');
}
document.write(removeTags('<html> <body> Javascript<body> is not Java'));;
</script>
</body>
</html>输出结果
Javascript is not Java
示例2
<html>
<body>
<script>
function removeTags(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace( /(<([^>]+)>)/ig, '');
}
document.write(removeTags('<html> Tutorix is <script> the best <body> e-learning platform'));;
</script>
</body>
</html>输出结果
Tutorix is the best e-learning platform
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短