Regular RegExp objects

Regular Expressions: specify the contents of text retrieval

  • Create a regular objects:
    (1) Constructor

    var reg1 = new RegExp("l");//一个参数,检索内容。
    var reg2 = new RegExp("l","ig");//两个参数。

    (2) literal manner

    var reg3=/l/ig;   //i:不区分大小写;g:全局匹配。
  • Retrieval methods:

    REG = var / L / IG;
    var str = "Hello";
    (. 1) reg.test (str) ; // str search character string contains the character pattern defined reg, returns a Boolean value.
    (2) reg.exec (STR) ; // implementation of specific search returns an array of [ "l", index: 3 , input: "hello"]; not found returns null.

  • Commonly used methods:
    (1) the compile to change the regular expression

    var reg1=/d/ig;   reg1.compile("o","g");  //reg1:/o/g;

    (2) match

    str.match(reg1);//[s,index:n,input:str];

    (. 3) Replace () Replace (sensitive spelling)

    str.replace("s1","**");//将str中的s1替换成**;

    (4) split

    str.split(reg1);
  • Metacharacters

    (1) Any of a character (except newline).
    (2) * 0 or more of any character (except newline).
    (3) at least one +.
    (4) {n1, n2} n1-n2 th. The / s {1,3} / match 1-3 s.
    (5) / [az] / ; az match from any one character. [A-zA-Z0-9]
    (. 6) / \ D /; // matching digital / \ D /; // not a number
    (7) / \ w /; // numbers, letters, underline _; / W / ; // In addition to numbers, letters, underline
    (8) / \ s /; // spaces; / \ S /; // non-space.
    (9) / ^ s /; // start with s; / s $ /; // end in s
    (10) / com | cn | net /; // or
    (11) in brackets () // groups: addition looking for character meet the conditions outside the parentheses will find an array of content into returns.
    (12)? Indicates that the preceding character zero or one. That is equivalent to {0,1}.

  • other

    RegExp.input last regular string matching.
    RegExp.lastMath last match to use regular string.
    reg.lastIndex should start looking for the next index.

Guess you like

Origin blog.51cto.com/11569511/2417752