Review of javascript Regular Expression learning materials

Source: Internet
Author: User

About reverse reference Copy codeThe Code is as follows: // test the Function
Function matchReg (reg, str ){
Var result = str. match (reg );
If (result ){
Console. dir (result );
} Else {
Console. log ('match failed ');
}
}

Var reg =/([A-Za-z] {0, 6}) \ 1 /;
Var str = 'andrewandrew ';
// Pass the test
MatchReg (reg, str );

// Pass (? : Pattern), does not record the content matched by the sub-expression (in this example, Andrew)
// Therefore, the reference to the matching content of the \ 1 subexpression fails.
// Note: The content matched by the subexpression is emphasized here, not the content of the subexpression itself.
Reg = /(? : [A-zA-Z] {0, 6}) \ 1 /;
// Test failed
MatchReg (reg, str );

Definition of subexpressions
Var parse_number =/^ -? \ D + (? : \. \ D *)? (? : E [+ \-]? \ D + )? $/I;
This is a regular expression used to parse numbers. The subexpressions include (? : \. \ D *) and (? : E [+ \-]? \ D +)
\. And \-are escape expressions for. And-respectively.
By the way, refer to other characters except line breaks.
-Generally used for [a-zA-Z0-9] to indicate the matching range
If no? For example (\. \ d *), the reverse reference relationship is as follows:
\ 1 --> (\. \ d *)
\ 2 --> (e [+ \-]? \ D +)
If there are more, and so on \ 3 \ 4 \ 5 ....
Again, it references the content matched by a subexpression, which is a specific text.
About forward pre-QueryCopy codeThe Code is as follows: var reg =/I like (? = Shanghai )/;
Var str = 'I like Shanghai ';
MatchReg (reg, str); // pass the test

Str = 'I like beijing ';
MatchReg (reg, str); // do not pass

Run the above Code and immediately understand what is forward pre-check. In the above example, through (? = Pattern), regular expression to predict whether the following content meets the requirements, if so, it will be matched smoothly.
Relatively ,(?! = Pattern) the usage intention is exactly the same (? = Pattern) on the contrary, we will not repeat it.
Greedy and non-Greedy matching ModesCopy codeThe Code is as follows: // greedy
Var reg =/\ d {1 ,}/;
Var str = '20140901 ';
MatchReg (reg, str); // result [0] is 1999. Multiple matches are allowed if multiple matches exist.

// Non-greedy
Reg =/\ d {1 ,}? /;
MatchReg (reg, str); // result [0] is 1 and only matches one

From the above results, we can easily see the meaning of "greedy" and "non-greedy ".
Pattern? This indicates that the non-Greedy match mode is generally greedy.
Result returned by the exec function of the Regexp objectCopy codeThe Code is as follows: // What is the returned result?
// The console. dir (result) in the matchReg function can indicate the problem. You can see it in firebug.
About the string replace Function
Function camelize (str ){
Return str. replace (/-(\ w)/g, function (inputStr, p1 ){
Console. log (p1 );
Return p1.toUpperCase ();
});
}
Console. log (camelize ('background-color '));

Function uncamelize (str, sep ){
Sep = sep | '-';
Return str. replace (/([a-z]) ([A-Z])/g, function (inputStr, p1, p2 ){
Console. log ('p1: % s, p2: % s', p1, p2 );
Return p1 + sep + p2;
});
}
Console. log (uncamelize ('backgroundcolor ','-'));

The above two examples are mainly used to describe the use of replace functions. Of course, functions such as search and split can take full advantage of regular expressions.
Best practices for javascript Regular Expressions
Just one sentence: Try to streamline and avoid complexity, which is conducive to reading and maintaining!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.