//把字符串中所有的s1替换为s2

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

//校验字符串是否为空
String.prototype.isEmpty = function () {
    return /^\s*$/.test(this);
}

//去除字符串左右两边的空格
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为开头
if (typeof(String.prototype.startWith) != "function") {
    String.prototype.startWith = function (str) {
        var reg = new RegExp("^" + str);
        return reg.test(this);
    }
}

//校验字符串是否以str为结尾
if (typeof(String.prototype.endWith) != "function") {
    String.prototype.endWith = function (str) {
        var reg = new RegExp(str + "$");
        return reg.test(this);
    }
}

//根据传入的字符串分组 比如str= ",,;;\n" 是根据字符串中 ,,;;\n 将字符串分割成数组
String.prototype.splitByStr = function (str) {
    var key = this;
    var reg = new RegExp("[" + str + "]");
    var arr = [];
    var tmp = reg.exec(key);
    while (tmp) {
        var tmpStr = key.substring(0, key.indexOf(tmp[0]));
        if (tmpStr.length > 0) {
            arr.push(tmpStr);
        }
        key = key.substring(tmpStr.length + 1);
        tmp = reg.exec(key);
    }
    arr.push(key);
    return arr;
}

 

posted on 2017-01-18 17:15  zdl524966159  阅读(183)  评论(0编辑  收藏  举报