JavaScript之字符串匹配工具[插件]
/*** * StringCheckUtil * 字符串检测工具 * * @version 1.0 * @method isNum(string,scope) * @method isChinese(string,scope) * @method isEnglish(string,scope) * @method isContainsString(string,subString,Case) */ /** * isNum(string,scope) * @param string:string resource * @param scope:all,part */ var StringCheckUtil = function() {}; StringCheckUtil.prototype.isNum = function(string, scope) { //检验参数合法性 if ((arguments.length != 2) || (typeof(string) != 'string') || (typeof(scope) != 'string')) { throw new Error("Arguments is not qualified!"); } //检验scope参数 if ((scope != 'part') && (scope != 'all')) { throw new Error("The argument what named is 'scope' is not qualified!"); } var regex_all = /\d*/; //i忽略大小写 var regex_part = /\d/gi; switch(scope){ case 'all': return (string.match(regex_all) == string) ? true : false;break; case 'part': return (regex_part.test(string));break; default: console.log('This string is unknown.'); } } /** * isContainsString(string,Case) * @param string:string resource * @param Case:i(case-insentitive):忽略大小写,c:(case)不忽略大小写 * */ StringCheckUtil.prototype.isContainsString = function(string, subString, Case) { //检验参数合法性 if ((arguments.length != 3) || (typeof(string) != 'string') || (typeof(subString) != 'string') || (typeof(Case) != 'string')) { throw new Error("Arguments' length is not qualified!"); } //检验scope参数 if ((Case != 'i') && (Case != 'c')) { throw new Error("The argument what named is 'scope' is not qualified!"); } var regex_case = (Case == 'i') ? 'i' : ''; var regex_scope = 'g'; //默认:全局 var regex = new RegExp(subString, regex_case + regex_scope); return regex.test(string); }
Demo:
var string_cs = "89898 67646g 6585dgdf7"; var stringNum = "jh787kk"; var Case_cs = 'i'; var subString_cs = "67646G"; var stringUtil_cs = new StringCheckUtil(); console.log("isNum:" + stringUtil_cs.isNum(stringNum, "all"));//非得全部是整数数字(不带小数点) console.log("isNum:" + stringUtil_cs.isNum(stringNum, "part"));//包含数字 console.log("isContainsString:" + stringUtil_cs.isContainsString(string_cs, subString_cs, Case_cs)); //忽略大小写,是否包含目标字符串
未完待续....
![QQ沟通交流群](https://blog-static.cnblogs.com/files/johnnyzen/cnblogs-qq-group-qrcode.gif?t=1679679148)
本文作者:
千千寰宇
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!