首页 > 代码库 > JavaScript之字符串匹配工具[插件]
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)); //忽略大小写,是否包含目标字符串
未完待续....
JavaScript之字符串匹配工具[插件]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。