日期:2014-05-16  浏览次数:20339 次

请教个js正则问题 去html内容中的空格
请教个js正则问题 如何去html内容中的空格
比如:
<html style=" xxx:xxx" ><div style=" xxx:xxx" class="xxxxx"> ssss ss ss ss </div></html>
让其变成
<html style=" xxx:xxx" ><div style=" xxx:xxx" class="xxxxx">ssssssssss</div></html>
或者文字前面的空格不去
<html style=" xxx:xxx" ><div style=" xxx:xxx" class="xxxxx"> ssssssssss</div></html>
不要去掉“html style”中的空格(标签中的属性的空格不要去掉)!
谢谢!

------解决方案--------------------
先获取文本再去空格
------解决方案--------------------
vat s=$(".xxxxx").text(); 
$(".xxxxx").text()=s.replace(/\s*/g, "");
------解决方案--------------------
'<html style=" xxx:xxx" ><div style=" xxx:xxx" class="xxxxx"> ssss ss ss ss </div></html>'.replace(/(?=[^>]*(?=<))\s+/g, "")
------解决方案--------------------
JScript code

var html="<html style=\" xxx:xxx\" ><div style=\" xxx:xxx\" class=\"xxxxx\"> ssss ss ss ss </div></html>";
    alert(html);
    html = html.replace(/\s+([^<>]+)(?=<)/g, function (m) { 
        return m.replace(/\s+/g,'');
    });
    alert(html);