js正则表达式 替换标签内的文本空白 本帖最后由 claymore1114 于 2013-12-19 11:16:57 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String.prototype.trim = function(){ return this.replace(/(^[\s\n\r]*)|([\s\n\r]*$)/g, ''); } var s = '<span style="color: #000000;"> 1 </span><span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3</b></span>'; s = s.replace(/>([^<]+)/g, function ($0, $1) { return '>' + $1.replace(/ /g, ' '); }); alert(s) String.prototype.trim1 = function(){ return this.split(" ").join(""); } <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><style type="text/css"></style></head><body><div id ="aa"> <span style="color: #000000;"> 1 </span> <span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3 <i>sd </i></b></span></div><script>var ele = document.getElementById("aa"), htm = ele.innerHTML; console.log(htm); htm = htm.replace(/\<span(?:\s+[^\>]+|)\>(.+)\<\/span\>/g,function($1,$2){ return $1.replace($2,$2.replace(/(?:^|\>)([^\<]+)(?:\<|$)/g,function($1,$2){ return $1.replace($2,$2.replace(/^\s+|\s+$/g," ")); })); }); ele.innerHTML = htm; console.log(htm);</script></body></html>试试这个行不。 地图轨迹显示 文本框提示信息 请问js能把浏览器的关闭键屏蔽掉吗? 在线等 路过的大哥、大姐来帮帮我这个小白吧。 过滤特殊字符, 为什么有的过滤不掉? 浏览器的confirm窗口可以指定缺省获得焦点的按钮为“取消”吗? 用javascript怎样实现一个主页记数器 一个关于javascript的问题 Span如何对齐? 如何隐去IE上方的菜单栏? js采集图片返回403 移动app中利用百度地图javascript API添加多个标注点的性能问题。
return this.replace(/(^[\s\n\r]*)|([\s\n\r]*$)/g, '');
}
var s = '<span style="color: #000000;"> 1 </span><span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3</b></span>';
s = s.replace(/>([^<]+)/g, function ($0, $1) { return '>' + $1.replace(/ /g, ' '); });
alert(s)
return this.split(" ").join("");
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
<div id ="aa">
<span style="color: #000000;"> 1 </span>
<span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3 <i>sd </i></b></span>
</div>
<script>
var ele = document.getElementById("aa"),
htm = ele.innerHTML;
console.log(htm);
htm = htm.replace(/\<span(?:\s+[^\>]+|)\>(.+)\<\/span\>/g,function($1,$2){
return $1.replace($2,$2.replace(/(?:^|\>)([^\<]+)(?:\<|$)/g,function($1,$2){
return $1.replace($2,$2.replace(/^\s+|\s+$/g," "));
}));
});
ele.innerHTML = htm;
console.log(htm);
</script>
</body>
</html>试试这个行不。