Common regular expression (phone number, email, URL address, ID card, etc.)

I. Introduction

  Bad habits: 1, used every regular Internet copy is a copy, nor to learn to think and see all mean;

        2, a project in different places use the same checksum, has been duplicated copy the code and did not unite, if one day you want to modify the rules, if people develop, regular check everyone is different, change up is very troublesome.

  First record it, slowly follow-up study, and finally he can read and write popular regular.

Second, the regular basic knowledge

  1, lateral fuzzy matching {} :

    Matching a regular length can not fixed character string, can be used to achieve this purpose quantifiers.

    Classifiers: for example {m, n}: m represents the minimum consecutive times, up to n times; followed defining a character to represent the characters.

    , Such as mail check: / ^ \ W @ + [A-Z0-9] + \ [az] {2,4} $ /, a write [az], it means there may have 2-4 letters;

    Such as the phone number verification: / ^. 1 [3456789] \ D {}. 9 $ /, followed the \ behind d, numeral 9 indicates the need arises       

{m, n}: m represents the minimum consecutive times, most n 
{m,}: indicates that an at least m 
{m}: indicates that an m times 
? : Equivalent to { 0,1 } indicates the presence or absence
 +: equivalent to {1 } indicates that an at least 1
 *: equivalent to {0} indicates appear any number of times, may not occur

    Such as mail check: / ^ \ W @ + [a-z0-9] + . \ [AZ] {2,4} $ /, written in the [a-z0-9] back, or can appear from 0 to 9 any one of the letters az, at least once

  2, the longitudinal fuzzy matching [] :

    For string matching the regular characters on a location, it may be many possible, is not limited to a certain type, character set can be used to achieve this purpose.

    Burst: such as [abc], indicating that a character is any character "a", "b", "c" in. For this position, we can have three possibilities.

    Such as the phone number verification: / ^. 1 [3456789] \ {D} $. 9 /, which in the second position, according to the general mobile phone operators to provide the second digital mobile phone may be any of 3-9 one was compliant.

    Particularly if the character in the character set represented by the notation may be used range, with a hyphen - to be omitted and abbreviations.

    Such as mail check: / ^ \ W @ + [A-Z0-9] . + \ [AZ] {2,4} $ /, @ behind it, seen according to the specification of our mailbox, is required after the @ 0 to 9 or any one of the letters az

  3, common shorthand

    \ d: represents [0-9 ], represents a digit 
    \ D: represents [ ^ 0-9 ] represents any character other than numbers outside 
    \ w: represents [ 0-9a-ZA- Z_], represents a number, size write letters and underscore 
    \ W: representation [ ^ 0-9a-zA-Z_], non-word character

    Such as the phone number verification: / ^. 1 [3456789] \ D {}. 9 $ /, binding behind quantifier, \ D in the third, from the third indicates, a total of nine numbers are

Third, the commonly used form

  The validation rules can be used in a project to write a tool in class, so you can check to ensure a project where the same thing is unity, do not repeat the excess pile of code.

 

the Validator {class
     // ID check 
    idCard (Val) {
         return / (^ [1-9] \ {D}. 5 (18 is |. 19 | ([23 is] \ D)) \ D {2} ((0 [ 1-9]) | (10 | 11 | 12)) (([0-2] [1-9]) | 10 | 20 | 30 | 31) \ d {3} [0-9Xx] $) | ( ^ [1-9] \ d {7 } ((0 [1-9]) | (10 | 11 | 12)) (([0-2] [1-9]) | 10 | 20 | 30 | 31 ) \ {D} $. 3) / .test (Val) 
    } 
    
    // phone number verification 
    phnoe (Val) {
         return / ^. 1 [3456789] \ {D}. 9 $ / .test (Val) 
    } 
    
    // email 
    email (Val) {
         return /^\w+@[a-z0-9]+\.[az]{2,4}$/ .test (Val) 
    } 
    
    // ordinary passport 
    passport (Val) {
         return / ^ (( . 1 [45] \ {D}. 7) | (G \ {D}. 8) | (P \ {D}. 7) | (S \ D {7, 8})) $ /?  .test (Val)
    }
    
    // MTPs 
    taiwanID (Val) {
         return / ^ [A-zA-the Z] [0-9] {}. 9 $ / .test (Val) 
    } 
    // Macau ID 
    hkId (Val) {
         return / ^ ( [AZ] \ D {6,10} (\ W1)?) $ / .test (Val) 
    } 
    
    // Chinese 
    chineseWord (Val) {
         return   / ^ [\ u4e00- \ u9fa5] * $ / .test (Val) 
    } 
    
    // password (not purely digital or letter) 
    psdRxp (Val) { 
        const numberRegexp = / ^ \ + $ D / ; 
        const letterRegexp = / ^ [A-zA-the Z] + $ / ;
         return numberRegexp.test (Val) ||  letterRegexp.test (Val)
    } 
} 
Exportdefault new Validator();

 

Bibliography: "javascript regular expression mini-book"

Guess you like

Origin www.cnblogs.com/songForU/p/12585344.html