Common regular expression and some demo

First, the check digit of expression

  • Digital: ^ [0-9] * $
  • The number n of bits: ^ \ d {n} $
  • At least an n-bit number: ^ \ d {n,} $
  • Mn-bit numbers: ^ \ d {m, n} $
  • Digital zero and non-zero at the beginning: ^ (0 | [1-9] [0-9] *) $
  • With up to two decimals beginning zero:? ^ ([1-9] [0-9] *) + (. \ [0-9] {1,2}) $
  • Decimals with 1-2 positive or negative:? ^ (\ -) \ d + (. \ \ D {1,2}) $
  • Positive, negative, and decimals:? ^ (\ - | \ +) \ d + (. \ \ D +) $?
  • Two positive real number of decimal places:? ^ [0-9] + (. \ [0-9] {2}) $
  • 1 to 3 a positive real number of decimals:? ^ [0-9] + (. \ [0-9] {1,3}) $
  • Nonzero positive integer: ^ [1-9] \ d * $ or ^ ([1-9] [0-9] *) or $ {1,3} ^ \ + [1-9] [0-? 9] * $
  • Negative integer zero: ^ \ - [1-9] [] 0-9 "or * ^ $ - [1-9] \ D * $
  • Non-negative integers: ^ \ d + $ or ^ [1-9] \ d * | 0 $
  • Non-positive integer: ^ - [1-9] \ d * | 0 $ or ^ ((- \ d +) | (0 +)) $
  • ? Non-negative float:. ^ \ D + (. \ \ D +) $ or ^ [1-9] \ d * \ \ d * | 0 \ \ d * [1-9] \ d * | 0 \? .0+ | 0 $
  • Non-positive float: ^ (.?.? (- \ d + (\ \ d +)) | (0 + (\ 0 +))) $ or ^ (- ([1-9] \ d * \ \ d. * |. 0 \ \ d * [1-9] \ d *)) | 0 \ 0+ |?. 0 $
  • Positive float: ^ [1-9] \ d * \ \ d * | 0 \ \ d * [1-9] \ d * $ ^ or (([0-9] + \ [0-9. ] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) |. ([0-9] * [1-9] [0-9] *)) $
  • Negative float: ^ - (.. [1-9] \ d * \ \ d * | 0 \ \ d * [1-9] \ d *) $ or ^ (- (([0-9] + \ . [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) |. ([ 0-9] * [1-9] [0-9] *))) $
  • Float: ^ (-? \ D +) (. \ \ D +) $ or ^ - ([1-9] \ d * \ \ d * | 0 \ \ d * [1-9] \ d??.. * | 0 \ 0+ |?. 0) $

Expressions check character

  • Characters: ^ [\ u4e00- \ u9fa5] {0,} $
  • Alphanumeric: ^ [A-Za-z0-9] + $ or ^ [A-Za-z0-9] {4,40} $
  • All characters in length is 3-20: {^} $ 3,20.
  • A string consisting of letters 26: ^ [A-Za-z] + $
  • String 26 from the English capital letters: ^ [AZ] + $
  • String consists of 26 lowercase English letters: ^ [az] + $
  • String of digits and letters of the English 26: ^ [A-Za-z0-9] + $
  • String of digits, letters or underscore 26 consisting of: ^ \ w + $ or ^ \ w {3,20} $
  • Chinese, English, numbers, including underscore: ^ [\ u4E00- \ u9FA5A-Za-z0-9 _] + $
  • Chinese, English, numbers, symbols such as but not underlined: ^ [\ u4E00- \ u9FA5A-Za-z0-9] + $ or ^ [\ u4E00- \ u9FA5A-Za-z0-9] {2,20} $
  • Input may contain% ^ & '?,; $ = \ "Characters such as: [^% &',; = $ \ x22?] +
  • - disable input characters comprising: [^ ~ \ x22] +

Third, the special needs of expression

  • Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
  • 域名: [a-zA-Z0-9] [- a-zA-Z0-9] {0,62} (/. [A-zA-Z0-9] [- a-zA-Z0-9] {0 , 62}) + /.
  • InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
  • Phone number: ^ (13 [0-9] | 14 [5 | 7] | 15 [0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9] | 18 [0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9]) \ d {8} $
  • 电话号码("XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
  • Domestic telephone number (0511-4405222,021-87888822): \ d {3} - \ d {8} | \ d {4} - \ d {7}
  • Phone number regular expression (support phone number, area code 3-4, 7-8 live numbers 1-4 digit extension number): ((\ d {11}) | ^ ((\ d {7,8} ) | (\ d {4} | \ d {3}) - (\ d {7,8}) | (\ d {4} | \ d {3}) - (\ d {7,8}) - (\ d {4} | \ d {3} | \ d {2} | \ d {1}) | (\ d {7,8}) - (\ d {4} | \ d {3} | \ d {2} | \ d {1})) $)
  • ID number (15, 18 digits), the last digit is a check digit, may be numbers or characters X: (^ \ d {15} $) | (^ \ d {18} $) | (^ \ d {17} (\ d | X | x) $)
  • Account is valid (beginning with the letter, allowing 5-16 bytes to allow alphanumeric underlined): ^ [a-zA-Z] [a-zA-Z0-9 _] {4,15} $
  • Password (start with a letter, a length between 6 and 18, can contain letters, numbers and underscores): ^ [a-zA-Z] \ w {5,17} $
  • Strong passwords (which must contain a combination of uppercase and lowercase letters and numbers, special characters can not be used, a length of between 8-10): ^ (?. = * \ D) (= * (= * [az]?.)?. [AZ]) [a-zA-Z0-9] {8,10} $
  • Strong passwords (which must contain a combination of uppercase and lowercase letters and numbers, special characters may be used, a length of between 8-10): ^ (?. = * \ D) (= * (= * [az]?.)?. [AZ]). {8,10} $
  • Date format: ^ \ d {4} - \ d {1,2} - \ d {1,2}
  • A year of 12 months (01 ~ 09 and 1 ~ 12): ^ (0 [1-9] | 1 [0-2]?) $
  • Month of 31 days (01 ~ 09 and 1 ~ 31): ^ ((0 [1-9]) | ((1 | 2) [0-9]) | 30 | 31?) $
  • Money input formats:
    1. There are four representations of money we can accept: "10000.00" and "10,000.00" and not "divide" and "10000" and "10,000": ^ [1-9] [0-9] * $
    2. This does not represent any of a number from 0 to begin with, but it also means that a character "0" is not passed, so we take the following form: ^ (0 | [1-9] [0-9] *) $
    3. A 0 or a number from 0 to not begin with we can also allow the beginning there is a minus: ^ (0 | - [1-9] [0-9] *?) $
    4. This may represent a 0 or a negative number 0 is not the beginning of the let users start with 0 as well. The minus sign also removed, because the money can not be negative right. Now we want to add is to explain fractional part may: (. [0-9] +)? ^ [0-9] + $
    5. It must be noted that, after the decimal point should be at least 1 digit, so "10" is not adopted, but "10" and "10.2" are adopted:. ^ [0-9] + ([0-9] {2})? $
    6. We must have regulations after the decimal point two, if you think too hard, it can be: (. [0-9] {1,2})? ^ [0-9] + $
    7. This allows a user to write only a decimal number following the comma we considered, we can:. ^ [0-9] {1,3} (, [0-9] {3}) * ([. 0-9] {1,2})? $
    8. 1 to 3 digits, followed by comma +3 arbitrary numbers, commas become optional, rather than necessary: ​​^ ([0-9] + | [0-9] {1,3} (, [0-9 ] {3}) *) (. [0-9] {1,2})? $
    9. Note: This is the final result, do not forget the "+" can use the "*" alternative if you feel empty string can accept it (wonder why?) Finally, do not forget to remove the remove function when using the backslash bar, general errors are here
  • xml Bunken: (? [a-zA-Z] + -). ^ + [a-zA-Z0-9] + \\ [x | X] [m | M] [l | L] $
  • Chinese characters of the regular expression: [\ u4e00- \ u9fa5]
  • Double Byte Character: [^ \ x00- \ xff] (including characters included, can be used to calculate the length of the string (a double-byte character length meter 2, ASCII characters are counted 1))
  • N blank lines of expression: \ n \ s * \ r (can be used to delete blank lines)
  • HTML markup regular expression:.?.? <(? \ S *) [^>] *> * | <* /> (inclusive blank character regular expression: ^ \ s * | \ s * $ or ( ^ \ s *) | (\ s * $) (blank character can be used to delete the beginning of the line end of the line (including spaces, tabs, page breaks, etc.), very useful expression)
  • Tencent QQ: [1-9] [0-9] {4,} (Tencent QQ number 10000 from the start)
  • China Postal Code: [1-9] \ d {5} (6-digit ZIP code China) (\ D?!)
  • IP address: (? (:( ?: 25 [0-5] | 2 [0-4] \\ d | [01] \\ d \\ d) \\??.) {3} (?: 25 [0-5] | 2 [0-4] \\ d |? [01] \\ d \\ d))?

Fourth, the front and back verification demo

        if((/^.*[\\/*:"?<>|].*$/).test(txtName)) {
            $ ( '# txtName') next () html.. ( ' title can not contain \\\ / *: ? "<> | and other arbitrary characters' );
            return false;
        } else {
            $('#txtName').next().html('');
        }
// Verify mailbox 
    public  static  boolean isEmail (String str) {
        String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        return match(regex,str);
    }
    
    // verify phone or fixed-line 
    public  static  boolean isTelephone (String str) {
        String regex = "^((0\\d{2,3}-\\d{7,8})|(1[34578]\\d{9}))$";
        return match(regex,str);
    }
    
    // validate zip code 
    public  static  boolean isPostCode (String str) {
        String regex = "^[0-9][0-9]{5}$";
        return match(regex,str);
    }
    
    // verify that the user name and company name input box general 
    public  static  Boolean isCheckColumn (String STR) {
        String regex = "^[\u4E00-\u9FA5A-Za-z0-9_]+$";
        return match(regex,str);
    }
    // add validation Books interface can not enter some input box <> 
    public  static  Boolean isCheckMediaColumn (String STR) {
        String regex = "^.*[<>].*$";
        return match(regex,str);
    }
    // add validation book title character input box can not enter illegal 
    public  static  boolean isCheckMediaName (String str) {
        String regex = "^.*[\\/*:\"?<>|].*$";
        return match(regex,str);
    }
    
            
    /**
    * @param regex
    * Regular expression string
    * @Param Cycle
    * To match the string
    * @Return if str comply with regex regular expression format, returns true, otherwise return false;
     * / 
    Private  static  boolean match (String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matches matches = pattern.matcher (str);
        Return matcher.matches ();
    }
// Special characters are replaced URL parameter may occur 
var replaceSpeciChaInReq = function (Key) {
     var replacedArr = [
                ['%','%25'],
                 [ '\\ +', '% 2B' ],
                 [' ','%20'],
                 [ '/', '% 2F' ],
                 [ '\\?', '% 3F' ],
                 ['#','%23'],
                 ['&','%26'],
                 [ '=', '% 3D' ],
                 ['<','&lt;'],
                 ['>','&gt;']
             ],reg='';
    for(var i=0;i<replacedArr.length;i++){
         reg=new RegExp(replacedArr[i][0],"g");
        key=key.replace(reg,replacedArr[i][1]);
    }
    return key;
}

 

Guess you like

Origin www.cnblogs.com/loong-hon/p/11124825.html