javaScript字符串工具类StringUtils详解
程序员文章站
2022-07-05 16:19:04
本文实例为大家分享了javascript字符串工具类的具体代码,供大家参考,具体内容如下
stringutils = {
isempty: functio...
本文实例为大家分享了javascript字符串工具类的具体代码,供大家参考,具体内容如下
stringutils = { isempty: function(input) { return input == null || input == ''; }, isnotempty: function(input) { return !this.isempty(input); }, isblank: function(input) { return input == null || /^\s*$/.test(input); }, isnotblank: function(input) { return !this.isblank(input); }, trim: function(input) { return input.replace(/^\s+|\s+$/, ''); }, trimtoempty: function(input) { return input == null ? "" : this.trim(input); }, startswith: function(input, prefix) { return input.indexof(prefix) === 0; }, endswith: function(input, suffix) { return input.lastindexof(suffix) === 0; }, contains: function(input, searchseq) { return input.indexof(searchseq) >= 0; }, equals: function(input1, input2) { return input1 == input2; }, equalsignorecase: function(input1, input2) { return input1.tolocalelowercase() == input2.tolocalelowercase(); }, containswhitespace: function(input) { return this.contains(input, ' '); }, //生成指定个数的字符 repeat: function(ch, repeattimes) { var result = ""; for(var i = 0; i < repeattimes; i++) { result += ch; } return result; }, deletewhitespace: function(input) { return input.replace(/\s+/g, ''); }, rightpad: function(input, size, padstr) { return input + this.repeat(padstr, size); }, leftpad: function(input, size, padstr) { return this.repeat(padstr, size) + input; }, //首小写字母转大写 capitalize: function(input) { var strlen = 0; if(input == null || (strlen = input.length) == 0) { return input; } return input.replace(/^[a-z]/, function(matchstr) { return matchstr.tolocaleuppercase(); }); }, //首大写字母转小写 uncapitalize: function(input) { var strlen = 0; if(input == null || (strlen = input.length) == 0) { return input; } return input.replace(/^[a-z]/, function(matchstr) { return matchstr.tolocalelowercase(); }); }, //大写转小写,小写转大写 swapcase: function(input) { return input.replace(/[a-z]/ig, function(matchstr) { if(matchstr >= 'a' && matchstr <= 'z') { return matchstr.tolocalelowercase(); } else if(matchstr >= 'a' && matchstr <= 'z') { return matchstr.tolocaleuppercase(); } }); }, //统计含有的子字符串的个数 countmatches: function(input, sub) { if(this.isempty(input) || this.isempty(sub)) { return 0; } var count = 0; var index = 0; while((index = input.indexof(sub, index)) != -1) { index += sub.length; count++; } return count; }, //只包含字母 isalpha: function(input) { return /^[a-z]+$/i.test(input); }, //只包含字母、空格 isalphaspace: function(input) { return /^[a-z\s]*$/i.test(input); }, //只包含字母、数字 isalphanumeric: function(input) { return /^[a-z0-9]+$/i.test(input); }, //只包含字母、数字和空格 isalphanumericspace: function(input) { return /^[a-z0-9\s]*$/i.test(input); }, //数字 isnumeric: function(input) { return /^(?:[1-9]\d*|0)(?:\.\d+)?$/.test(input); }, //小数 isdecimal: function(input) { return /^[-+]?(?:0|[1-9]\d*)\.\d+$/.test(input); }, //负小数 isnegativedecimal: function(input) { return /^\-?(?:0|[1-9]\d*)\.\d+$/.test(input); }, //正小数 ispositivedecimal: function(input) { return /^\+?(?:0|[1-9]\d*)\.\d+$/.test(input); }, //整数 isinteger: function(input) { return /^[-+]?(?:0|[1-9]\d*)$/.test(input); }, //正整数 ispositiveinteger: function(input) { return /^\+?(?:0|[1-9]\d*)$/.test(input); }, //负整数 isnegativeinteger: function(input) { return /^\-?(?:0|[1-9]\d*)$/.test(input); }, //只包含数字和空格 isnumericspace: function(input) { return /^[\d\s]*$/.test(input); }, iswhitespace: function(input) { return /^\s*$/.test(input); }, isalllowercase: function(input) { return /^[a-z]+$/.test(input); }, isalluppercase: function(input) { return /^[a-z]+$/.test(input); }, defaultstring: function(input, defaultstr) { return input == null ? defaultstr : input; }, defaultifblank: function(input, defaultstr) { return this.isblank(input) ? defaultstr : input; }, defaultifempty: function(input, defaultstr) { return this.isempty(input) ? defaultstr : input; }, //字符串反转 reverse: function(input) { if(this.isblank(input)) { input; } return input.split("").reverse().join(""); }, //删掉特殊字符(英文状态下) removespecialcharacter: function(input) { return input.replace(/[!-/:-@\[-`{-~]/g, ""); }, //只包含特殊字符、数字和字母(不包括空格,若想包括空格,改为[ -~]) isspecialcharacteralphanumeric: function(input) { return /^[!-~]+$/.test(input); }, /** * 校验时排除某些字符串,即不能包含某些字符串 * @param {object} conditions:里面有多个属性,如下: * * @param {string} matcherflag 匹配标识 * 0:数字;1:字母;2:小写字母;3:大写字母;4:特殊字符,指英文状态下的标点符号及括号等;5:中文; * 6:数字和字母;7:数字和小写字母;8:数字和大写字母;9:数字、字母和特殊字符;10:数字和中文; * 11:小写字母和特殊字符;12:大写字母和特殊字符;13:字母和特殊字符;14:小写字母和中文;15:大写字母和中文; * 16:字母和中文;17:特殊字符、和中文;18:特殊字符、字母和中文;19:特殊字符、小写字母和中文;20:特殊字符、大写字母和中文; * 100:所有字符; * @param {array} excludestrarr 排除的字符串,数组格式 * @param {string} length 长度,可为空。1,2表示长度1到2之间;10,表示10个以上字符;5表示长度为5 * @param {boolean} ignorecase 是否忽略大小写 * conditions={matcherflag:"0",excludestrarr:[],length:"",ignorecase:true} */ ispatternmustexcludesomestr: function(input, conditions) { //参数 var matcherflag = conditions.matcherflag; var excludestrarr = conditions.excludestrarr; var length = conditions.length; var ignorecase = conditions.ignorecase; //拼正则 var size = excludestrarr.length; var regex = (size == 0) ? "^" : "^(?!.*(?:{0}))"; var subpattern = ""; for(var i = 0; i < size; i++) { excludestrarr[i] = bee.stringutils.escapemetacharacterofstr(excludestrarr[i]); subpattern += excludestrarr[i]; if(i != size - 1) { subpattern += "|"; } } regex = this.format(regex, [subpattern]); switch(matcherflag) { case '0': regex += "\\d"; break; case '1': regex += "[a-za-z]"; break; case '2': regex += "[a-z]"; break; case '3': regex += "[a-z]"; break; case '4': regex += "[!-/:-@\[-`{-~]"; break; case '5': regex += "[\u4e00-\u9fa5]"; break; case '6': regex += "[a-za-z0-9]"; break; case '7': regex += "[a-z0-9]"; break; case '8': regex += "[a-z0-9]"; break; case '9': regex += "[!-~]"; break; case '10': regex += "[0-9\u4e00-\u9fa5]"; break; case '11': regex += "[a-z!-/:-@\[-`{-~]"; break; case '12': regex += "[a-z!-/:-@\[-`{-~]"; break; case '13': regex += "[a-za-z!-/:-@\[-`{-~]"; break; case '14': regex += "[a-z\u4e00-\u9fa5]"; break; case '15': regex += "[a-z\u4e00-\u9fa5]"; break; case '16': regex += "[a-za-z\u4e00-\u9fa5]"; break; case '17': regex += "[\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '18': regex += "[\u4e00-\u9fa5!-~]"; break; case '19': regex += "[a-z\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '20': regex += "[a-z\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '100': regex += "[\s\s]"; break; default: alert(matcherflag + ":this type is not supported!"); } regex += this.isnotblank(length) ? "{" + length + "}" : "+"; regex += "$"; var pattern = new regexp(regex, ignorecase ? "i" : ""); return pattern.test(input); }, /** * @param {string} message * @param {array} arr * 消息格式化 */ format: function(message, arr) { return message.replace(/{(\d+)}/g, function(matchstr, group1) { return arr[group1]; }); }, /** * 把连续出现多次的字母字符串进行压缩。如输入:aaabbbbcccccd 输出:3a4b5cd * @param {string} input * @param {boolean} ignorecase : true or false */ compressrepeatedstr: function(input, ignorecase) { var pattern = new regexp("([a-z])\\1+", ignorecase ? "ig" : "g"); return result = input.replace(pattern, function(matchstr, group1) { return matchstr.length + group1; }); }, /** * 校验必须同时包含某些字符串 * @param {string} input * @param {object} conditions:里面有多个属性,如下: * * @param {string} matcherflag 匹配标识 * 0:数字;1:字母;2:小写字母;3:大写字母;4:特殊字符,指英文状态下的标点符号及括号等;5:中文; * 6:数字和字母;7:数字和小写字母;8:数字和大写字母;9:数字、字母和特殊字符;10:数字和中文; * 11:小写字母和特殊字符;12:大写字母和特殊字符;13:字母和特殊字符;14:小写字母和中文;15:大写字母和中文; * 16:字母和中文;17:特殊字符、和中文;18:特殊字符、字母和中文;19:特殊字符、小写字母和中文;20:特殊字符、大写字母和中文; * 100:所有字符; * @param {array} excludestrarr 排除的字符串,数组格式 * @param {string} length 长度,可为空。1,2表示长度1到2之间;10,表示10个以上字符;5表示长度为5 * @param {boolean} ignorecase 是否忽略大小写 * conditions={matcherflag:"0",containstrarr:[],length:"",ignorecase:true} * */ ispatternmustcontainsomestr: function(input, conditions) { //参数 var matcherflag = conditions.matcherflag; var containstrarr = conditions.containstrarr; var length = conditions.length; var ignorecase = conditions.ignorecase; //创建正则 var size = containstrarr.length; var regex = "^"; var subpattern = ""; for(var i = 0; i < size; i++) { containstrarr[i] = bee.stringutils.escapemetacharacterofstr(containstrarr[i]); subpattern += "(?=.*" + containstrarr[i] + ")"; } regex += subpattern; switch(matcherflag) { case '0': regex += "\\d"; break; case '1': regex += "[a-za-z]"; break; case '2': regex += "[a-z]"; break; case '3': regex += "[a-z]"; break; case '4': regex += "[!-/:-@\[-`{-~]"; break; case '5': regex += "[\u4e00-\u9fa5]"; break; case '6': regex += "[a-za-z0-9]"; break; case '7': regex += "[a-z0-9]"; break; case '8': regex += "[a-z0-9]"; break; case '9': regex += "[!-~]"; break; case '10': regex += "[0-9\u4e00-\u9fa5]"; break; case '11': regex += "[a-z!-/:-@\[-`{-~]"; break; case '12': regex += "[a-z!-/:-@\[-`{-~]"; break; case '13': regex += "[a-za-z!-/:-@\[-`{-~]"; break; case '14': regex += "[a-z\u4e00-\u9fa5]"; break; case '15': regex += "[a-z\u4e00-\u9fa5]"; break; case '16': regex += "[a-za-z\u4e00-\u9fa5]"; break; case '17': regex += "[\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '18': regex += "[\u4e00-\u9fa5!-~]"; break; case '19': regex += "[a-z\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '20': regex += "[a-z\u4e00-\u9fa5!-/:-@\[-`{-~]"; break; case '100': regex += "[\s\s]"; break; default: alert(matcherflag + ":this type is not supported!"); } regex += this.isnotblank(length) ? "{" + length + "}" : "+"; regex += "$"; var pattern = new regexp(regex, ignorecase ? "i" : ""); return pattern.test(input); }, //中文校验 ischinese: function(input) { return /^[\u4e00-\u9fa5]+$/.test(input); }, //去掉中文字符 removechinese: function(input) { return input.replace(/[\u4e00-\u9fa5]+/gm, ""); }, //转义元字符 escapemetacharacter: function(input) { var metacharacter = "^$()*+.[]|\\-?{}|"; if(metacharacter.indexof(input) >= 0) { input = "\\" + input; } return input; }, //转义字符串中的元字符 escapemetacharacterofstr: function(input) { return input.replace(/[\^\$\*\+\. \|\\\-\?\{\}\|]/gm, "\\$&"); } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。