如何从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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短