Detailed explanation of javaScript string tool class StringUtils
本文主要为大家详细介绍了javaScript字符串工具类StringUtils,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。
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, "\\$&"); } };
相关推荐:
The above is the detailed content of Detailed explanation of javaScript string tool class StringUtils. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.
