JS正则匹配style,width,height等属性
str=str.replace(/[ t]*style[ t]*=[ t]*("[^"]+")|('[^']+')/ig,"");
str=str.replace(/[ t]*width[ t]*=[ t]*("[^"]+")|('[^']+')/ig,"");
str=str.replace(/[ t]*height[ t]*=[ t]*("[^"]+")|('[^']+')/ig,"");
str=str.replace(/[ t]*width[ t]*=[ t]*[^ t]+/ig,"");
str=str.replace(/[ t]*height[ t]*=[ t]*[^ t]+/ig,"");
使用实例
图片自适应问题,保留宽度,删除高度
html部分
<body onload='clearheight()'>
<div id='con'>
<p>这里是内容,很多图片内容啊<p>
</div>
</body>
js部分
<script type="text/javascript">
function clearheight()
{
var e=document.getElementById("con");
var scon=e.innerHTML;
scon = scon.replace(/[ t]*height[ t]*=[ t]*("[^"]+")|('[^']+')/ig,"");
scon = scon.replace(/[ t]*height[ t]*=[ t]*[^ t]+/ig,"");
e.innerHTML=str1;
}
</script>