在网上找了好久,发现没几个是好用的。自己备份一个省得以后再找。-、-
//去左空格;
function ltrim(s){
return s.replace( /^/s*/, "");
}
//去右空格;
function rtrim(s){
return s.replace( //s*$/, "");
}
//左右空格;
function trim(s){
return rtrim(ltrim(s));
}
如果是去掉半角和全角空格就把 /s 替换成 [" "|" "] 就变成
//去左空格;
function ltrim(s){
return s.replace( /^[" "|" "]*/, "");
}
//去右空格;
function rtrim(s){
return s.replace( /[" "|" "]*$/, "");
}
//左右空格;
function trim(s){
return rtrim(ltrim(s));
}
<script language=Javascript>
String.prototype.Trim = function(){return this.replace(/^/s+|/s+$/g,"");}
String.prototype.Ltrim = function(){return this.replace(/^/s+/g, "");}
String.prototype.Rtrim = function(){return this.replace(//s+$/g, "");}
var str = " sdf ";
alert(str.Trim());
alert(str.Ltrim());
alert(str.Rtrim());
</script>