• 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));
        //忽略大小写,是否包含目标字符串
    

      

    未完待续....

  • 相关阅读:
    Linux常用命令大全(非常全!!!)
    洛谷 P3379 【模板】最近公共祖先(LCA)
    POJ 3259 Wormholes
    POJ 1275 Cashier Employment
    POJ 3169 Layout
    POJ 1201 Intervals
    洛谷 P5960 【模板】差分约束算法
    洛谷 P3275 [SCOI2011]糖果
    POJ 2949 Word Rings
    POJ 3621 Sightseeing Cows
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/7208981.html
Copyright © 2020-2023  润新知