js系统函数扩展-String
$.extend(String.prototype, {
trim:function () {/*删除首尾空格*/
return this.replace(/^\s*|\s*$/g, "");
},
isAlpha: function () {/*是否由数字、字母、下划线组成*/
var reg = /^\w+$/g;
return reg.test(this);
},
isChinese: function () {/*是否是汉字*/
var reg = /^[\u4E00-\u9FA5]+$/g;
return reg.test(this)
},
containsChinese: function () {/*是否包含汉字*/
var reg = /[\u4E00-\u9FA5]/g;
return reg.test(this)
},
isNumber: function () {/*是否是数字*/
var reg = /^([1-9]\d*|0)$/;
return reg.test(this);
},
containsNumber: function () {/*是否包含数字*/
var reg = /[0-9]/g;
return reg.test(this);
},
isLetter: function () {/*是否是字母*/
var reg = /^[a-zA-Z]+$/g;
return reg.test(this);
},
isUrl: function (url) {/*是否是url*/
url = this.isNullOrEmpty(url) ? "" : String(url);
var strRegex = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
var re = new RegExp(strRegex);
return re.test(url);
},
isNullOrEmpty: function (str) {/*是否为空*/
return str === undefined || str === null || str === "";
},
containsLetter : function () { //是否包含字母
var reg = /[a-zA-Z]/g;
return reg.test(this);
},
isEmail: function () {/*是否是邮箱地址*/
var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g;
return reg.test(this);
},
getByteCount: function () {/*返回字节数*/
return this.replace(/[^\x00-\xff]/g, "**").length;
},
});
/*串联字符串数组所有元素*/
String.join = function (separator, list) {
var str = "";
if ($.isArray(list)) {
for (var i = 0,length=list.length; i < length; i++) {
if (i < length - 1) {
str += list[i] + separator;
}
else {
str += list[i];
}
}
}
return str;
};