当前位置:编程学习 > JS >>

用JS过滤HTML的正则表达式函数

<script type="text/javascript">
var obj = "<a href=\"http://www.zhaoxi.net\"><div>banner制作</div>89</a>";
obj = obj.replace(/<\/?[^>]*>/g,''); //去除HTML tag
obj = obj.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
obj = obj.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
document.write(obj);
</script>

<input name="s1" id="s1" type="text" value="" onblur="jj();" onkeyup="jj();">
<script type="text/javascript">
function jj(){
var a=document.getElementById('s1');
a.value = a.value.replace(/<\/?[^>]+>/g,''); //去除HTML tag
a.value = a.value.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
a.value= a.value.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
}
</script>

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,