<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<SCRIPT LANGUAGE="JavaScript">
String.prototype.Trimheadfoot = function() {
return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.Trimmiddle = function() {
return this.replace(/(\s)\1+/g, "$1").replace(/(\r\n)\1+/g, "$1");
}
//-->
</SCRIPT> </head>
<body>
<textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
<input type="button" value="计算字数">
</body>
</html>
str.replace(/\n{2,}/g,"\n").replase(/\s{2,}/g," ");
String.prototype.Trimheadfoot = function() { return this.replace(/(^\s*)|(\s*$)/g,""); }
String.prototype.Trimmiddle = function() { return this.replace(/\s{2,}|[\r\n]{2,}/g, "$1"); }
</script>
String.prototype.Trimheadfoot = function() {
return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.Trimmiddle = function() {
return this.replace(/((?:\r\n)|\s)\1+/g, "$1");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var str = "abc abc\r\n\r\n\r\nabc abc";
alert(str);
str = str.replace(/(\s)(\r*\1)+/g,"$1");
alert(str);
</script>
</body>
</html>
var str =" abd def 123 \n\n xyj\n\nkkkl adfl";
alert(str);
alert(str.replace(/\n{2,}/g,"\n").replace(/[ ]{2,}/g," "));
String.prototype.Trimheadfoot = function() { return this.replace(/(^\s*)|(\s*$)/g,""); }
String.prototype.Trimmiddle = function() { return this.replace(/(\s|\r\n)\1+/gi, "$1"); }
</script>
<textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
<input type="button" value="计算字数">
if(!c || c==' '){
return this.replace(/(^\s*)|(\s*$)/g, "");
}else{
var s = this;
var i, b = 0, e = s.length;
for(i=0;i<s.length;i++)
if(s.charAt(i) != c){ b=i; break; }
if(i==s.length)return "";
for(i = s.length-1;i>b;i--)
if(s.charAt(i) != c){ e=i; break; }
return s.substring(b,e+1);
}
}
接受传入参数,相当于C#中的Trim
再发一个。var str =" abd def 123 \r\n\r\n xyj\r\n\nkkkl adfl";
alert(str);
alert(str.replace(/[ ]{2,}/g," ").replace(/\s{2,}/g,"\n"));
各位大大出bug了!!!!!
bug:当遇到回车后再打空格,就会出bug,变得不符合需求
没问题啊,你没试过我写的么?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<textarea id="txt" style="widht:400px;height:400px;"></textarea>
<script type="text/javascript">
var txt = document.getElementById("txt");
var str = "abc abc\n\n\n abc abc";
txt.value = "替换之前:\r\n" + str + "\r\n";//替换
str = str.replace(/(\s)(\r*\1)+/g,"$1");
txt.value = txt.value + "替换之后:\r\n" + str;
</script>
</body>
</html>
alert(str);
alert(str.replace(/^\s+|\s+$/g,"").replace(/[ ]{2,}/g," ").replace(/\s{2,}/g,"\n"));
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<textarea id="txt" style="widht:400px;height:400px;"></textarea>
<script type="text/javascript">
//写成函数了
String.prototype.Trim = function() {
return this.replace(/([\r\n ]{2,})/g,function(a){return a.indexOf("\n")>=0?"\r\n":" "});
} //测试代码
var txt = document.getElementById("txt");
var str = "abc abc\n\n\n abc abc";
txt.value = "替换之前:\r\n" + str + "\r\n";
str = str.Trim();
txt.value = txt.value + "替换之后:\r\n" + str;
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<textarea id="txt" style="widht:400px;height:400px;"></textarea>
<script type="text/javascript">
//写成函数了
String.prototype.Trim = function() {
return this.replace(/[ ]{2,}/g," ").replace(/(\s|[ ]){2,}/g,"\n");
} //测试代码
var txt = document.getElementById("txt");
var str = "abc abc\n\n\n \n\r adsflkj \n\r \n\r abc abc";
txt.value = "替换之前:\r\n" + str + "\r\n";
str = str.Trim();
txt.value = txt.value + "替换之后:\r\n" + str;
</script>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<SCRIPT LANGUAGE="JavaScript">
String.prototype.Trimheadfoot = function() {
return this.replace(/(^\s*)|(\s*$)/g,"");
}
String.prototype.Trimmiddle = function() {
return this.replace(/(?:(\r\n)|\s)\s+/g, "\r\n");
}
</SCRIPT> </head>
<body>
<textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
<input type="button" value="计算字数">
</body>
</html>