JavaScript之字串匹配工具[外掛程式]

來源:互聯網
上載者:User

標籤:err   row   arguments   小數   switch   throw   script   false   ons   

/***     * 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.