(^\s+)|(\s+$)/g
/ pattern /g
是js正则字符串的语法
g (全文查找出现的所有 pattern)
i (忽略大小写)
m (多行查找)
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,"")}
//批量替换,比如:str.ReplaceAll([/a/g,/b/g,/c/g],["aaa","bbb","ccc"])
String.prototype.ReplaceAll=function (A,B) {
var C=this;
for(var i=0;i<A.length;i++) {
C=C.replace(A[i],B[i]);
};
return C;
};
// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str)
{
return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
return this.replace(/(^[\\s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
return this.replace(/([\\s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}