javaScript文字列ツール類StrigUtils詳細
15108 ワード
本論文の例では、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, "\\$&");
}
};
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。