search
HomeWeb Front-endJS TutorialHow to skillfully use the string tool class StringUtils in javaScript

这篇文章主要为大家详细介绍了javaScript字符串工具类StringUtils,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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, &#39;&#39;); 
  }, 
  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 >= &#39;A&#39; && matchStr <= &#39;Z&#39;) { 
     return matchStr.toLocaleLowerCase(); 
    } else if(matchStr >= &#39;a&#39; && matchStr <= &#39;z&#39;) { 
     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 &#39;0&#39;: 
     regex += "\\d"; 
     break; 
    case &#39;1&#39;: 
     regex += "[a-zA-Z]"; 
     break; 
    case &#39;2&#39;: 
     regex += "[a-z]"; 
     break; 
    case &#39;3&#39;: 
     regex += "[A-Z]"; 
     break; 
    case &#39;4&#39;: 
     regex += "[!-/:-@\[-`{-~]"; 
     break; 
    case &#39;5&#39;: 
     regex += "[\u4E00-\u9FA5]"; 
     break; 
    case &#39;6&#39;: 
     regex += "[a-zA-Z0-9]"; 
     break; 
    case &#39;7&#39;: 
     regex += "[a-z0-9]"; 
     break; 
    case &#39;8&#39;: 
     regex += "[A-Z0-9]"; 
     break; 
    case &#39;9&#39;: 
     regex += "[!-~]"; 
     break; 
    case &#39;10&#39;: 
     regex += "[0-9\u4E00-\u9FA5]"; 
     break; 
    case &#39;11&#39;: 
     regex += "[a-z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;12&#39;: 
     regex += "[A-Z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;13&#39;: 
     regex += "[a-zA-Z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;14&#39;: 
     regex += "[a-z\u4E00-\u9FA5]"; 
     break; 
    case &#39;15&#39;: 
     regex += "[A-Z\u4E00-\u9FA5]"; 
     break; 
    case &#39;16&#39;: 
     regex += "[a-zA-Z\u4E00-\u9FA5]"; 
     break; 
    case &#39;17&#39;: 
     regex += "[\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;18&#39;: 
     regex += "[\u4E00-\u9FA5!-~]"; 
     break; 
    case &#39;19&#39;: 
     regex += "[a-z\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;20&#39;: 
     regex += "[A-Z\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;100&#39;: 
     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 &#39;0&#39;: 
     regex += "\\d"; 
     break; 
    case &#39;1&#39;: 
     regex += "[a-zA-Z]"; 
     break; 
    case &#39;2&#39;: 
     regex += "[a-z]"; 
     break; 
    case &#39;3&#39;: 
     regex += "[A-Z]"; 
     break; 
    case &#39;4&#39;: 
     regex += "[!-/:-@\[-`{-~]"; 
     break; 
    case &#39;5&#39;: 
     regex += "[\u4E00-\u9FA5]"; 
     break; 
    case &#39;6&#39;: 
     regex += "[a-zA-Z0-9]"; 
     break; 
    case &#39;7&#39;: 
     regex += "[a-z0-9]"; 
     break; 
    case &#39;8&#39;: 
     regex += "[A-Z0-9]"; 
     break; 
    case &#39;9&#39;: 
     regex += "[!-~]"; 
     break; 
    case &#39;10&#39;: 
     regex += "[0-9\u4E00-\u9FA5]"; 
     break; 
    case &#39;11&#39;: 
     regex += "[a-z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;12&#39;: 
     regex += "[A-Z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;13&#39;: 
     regex += "[a-zA-Z!-/:-@\[-`{-~]"; 
     break; 
    case &#39;14&#39;: 
     regex += "[a-z\u4E00-\u9FA5]"; 
     break; 
    case &#39;15&#39;: 
     regex += "[A-Z\u4E00-\u9FA5]"; 
     break; 
    case &#39;16&#39;: 
     regex += "[a-zA-Z\u4E00-\u9FA5]"; 
     break; 
    case &#39;17&#39;: 
     regex += "[\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;18&#39;: 
     regex += "[\u4E00-\u9FA5!-~]"; 
     break; 
    case &#39;19&#39;: 
     regex += "[a-z\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;20&#39;: 
     regex += "[A-Z\u4E00-\u9FA5!-/:-@\[-`{-~]"; 
     break; 
    case &#39;100&#39;: 
     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, "\\$&"); 
  } 
 
 };

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

Web表单的JS插件(精品推荐)

在AngularJS中如何实现猜数字大小功能

在jQuery中图片查看插件如何使用

在vue中如何实现分页组件

使用vue实现简单键盘操作

The above is the detailed content of How to skillfully use the string tool class StringUtils in javaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),