JavaScript- regular

Regular introduction
js appears: Form Validation
Regular Expressions:
  A character rules described objects , can check whether a character string containing a character, the character matching substrings do replaced or removed from a condition in a string like.
Regular grammar based on an ancient language Perl
 
Regular use and create
1. Use:
  Can not be used directly, with the method, it is generally used to verify the regular characters
Character: str.match (reg) str.replace (reg) str.search ()
Regular: reg.test (str)
 
2. Regular features:
  Screened qualified string, replacing qualified string of characters verify compliance with rules
 
3. Create
1) a constructor argument:    
var REG1 = new new the RegExp ( "a" );
 var STR = "adassdfsd" ; 
 the console.log (str.match (REG1))         // output matching a character

2) literal way:

 var REG2 = / a / ; 
the console.log (str2.match (REG2));       // the output of a matching character
   / / Is a regular expression identifier
      "" Is a string identifier
      [] Is an array of identifiers
      {} Is the identifier of the object
 
Regular verification:
    var reg = / [0-9] /;
    reg.test (str) // returns true if the verification is successful, false indicates failure
 
String .replace (oldstr, newstr) function and string .match (regular) function
1) Function of the replace function to replace one string :( only, and the case has not replaced)
Will replace the old string to the new string
 
2) match the string of functionality:
Extract all the things you want,  it returns an array containing all eligible characters
 
Regular content:
Modifiers:
Canonical regularization of modifiers, written on the back of expression / n:
G : represents the global match finds all
i : ignore case represents
var   STR = "abYchdYewyzyhcyaq" 
the console.log (str.replace ( / Y / G, "Yu"))    // abYchdYew z Yu Yu Yu hc AQ 
the console.log (str.replace (/ Y / GI, "Yu") )    // ab & chd Yu Yu ew z Yu Yu Yu aq hc

Quantifier: Qualifier

 Represents at least one uncapped

()   A separate part

[]    A separate part, but the interior of the Meta content is character or relationship, a Ghost character, one character can match

   Inside the symbol represents a non Ghost, except

|   Or         ? Zero or 1            *   zero or more times           

{n} to match at least n times       {n, m} matches at least n times, matching up m times

Note: After the qualifier followed? , Matched by greed become non-greedy match
 
Escapes:
  \    D - [0-9] Digital
  \ W    - [A-z0-9_] numbers, letters, underline
 \    S - blank characters (spaces)    
 \ D    - [^ 0-9] non-numeric
 \ W is    - [^ A-z0-9_] non-numeric, alphabetic, underscore
    S - non-whitespace characters

Representatives of all (not recommended)
var   REG = / <. +> / G       // filtration, angle brackets represents something not 
console.log (str.replace (reg. "" ))
  Regular greed Law:  
var REG = / <[^ <>] +> / G 
the console.log (str.replace (REG, "")) // represents the angle brackets do something really

 

Modify the partial authentication:

We need to give regular expression, coupled with the beginning and end, indicating the end of validation from the beginning of the string to string

^ Indicates the beginning

$ Represents the end

 

Commonly used detection method:
1. Chinese Detection
    unicode encoded Chinese surveillance: / ^ [\ u2E80- \ u9FFF] + $ /
 
2. User name detection
    Regular: / ^ [a-z0-9 _-] {3,16} $ /
 
3. mail detection
    /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
      Can appear multiple times (underlined alphanumeric .-) may occur multiple @ (.- alphanumeric). 2-6 or letters.
    /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/
There may be multiple (alphanumeric) may have a plurality of free (plurality (alphanumeric)) may be a plurality @ (alphanumeric there may be no 1 (- alphanumeric)) can have multiple (2 or 1 a plurality of letters)
 
4.URL detection
    /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?$/
There are 1 or 0 times (http times with a 1 or 0 s: //) may be a plurality (alphanumeric .-) 2-6 (letters.) May be 0 or more (or a plurality may be 0. / .- alphanumeric underlined) may be 0 or 1 /
 
 
5.HTML label inspection
    /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
<Plurality (letters), or may be a plurality of 0 (all the characters except <a) />
<Plurality (letters), or may be a plurality of 0 (all the characters except <a)> any number of characters </ repetition of the first portion of the plurality of letters>
 
6. replace a character in all the special characters with a positive:
/ [ \w_ ]+ /g
 
 
 

Guess you like

Origin www.cnblogs.com/hcy08042/p/11426604.html