如何从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 红包祝福语简短10字
18 周六早晨祝福语简短