我们可以使用javascript中的正则表达式删除字符串中的HTML / XML 标签。HTML元素(例如span,div等)出现在左右箭头之间,例如<div>,<span>等。因此,用nothing('')替换箭头中的内容以及箭头,可以完成我们的任务简单。
str.replace( /(<([^>]+)>)/ig, '');
<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
<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