Regular expression Learning for JavaScript

Source: Internet
Author: User

About reverse referencing

Copy the code code as follows:


Test 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 ';
Test Pass
Matchreg (Reg, str);

by (?:p Attern), the content of sub-expression matching is not recorded (Andrew in this case)
So the reference to the \1 to match the content of the sub-expression failed.
Note: The emphasis here is on what the sub-expression matches, not on the sub-expression itself
Reg =/(?: [a-za-z]{0,6}) \1/;
Test does not pass
Matchreg (Reg, str);


About definitions of sub-expressions
var parse_number =/^-?\d+ (?: \. \d*)? (?: e[+\-]?\d+)? $/i;
This is a regular expression that parses a number, where the subexpression has (?: \. \d*) and (?: e[+\-]?\d+)
\. and \-are the escape expressions for. and-respectively.
By the way. Represents characters other than line break
-Generally used for [a-za-z0-9] to indicate a matching range
If not: for example (\.\d*) then in order of precedence, the relationship of the inverse reference is as follows
\1--> (\.\d*)
\2--> (e[+\-]?\d+)
If there are more words, then \3 \4 \5 ....
Again, the reference to the sub-expression matches the content, is the specific text
About forward pre-check

Copy the code code as follows:


var reg =/I like (? =shanghai)/;
var str = ' I like Shanghai ';
Matchreg (Reg, str); Test Pass

str = ' I like Beijing ';
Matchreg (Reg, str); does not pass


Run down the code, immediately understand what is positive pre-check, as the above example, through the (? =pattern) Form, the regular expression prediction next to the content is not meet the requirements, if it is the smooth match.
In contrast, (?! =pattern) is intended to be used in the opposite direction (? =pattern).
On greedy and non-greedy matching patterns

Copy the code code as follows:


Greedy
var reg =/\d{1,}/;
var str = ' 1999 ';
Matchreg (Reg, str); Result[0] Multiple matches for 1999 can match

Non-greedy
reg =/\d{1,}?/;
Matchreg (Reg, str); Result[0] for 1 matches only one


From the above results it is easy to see the meaning of "greedy" and "non-greedy"
Pattern? This shows the non-greedy matching pattern, the general situation is greedy
About the results returned by the RegExp object exec function

Copy the code code as follows:


What is the result of the return?
Console.dir (Result) in the Matchreg function can explain the problem, Firebug
About the Replace function of string
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 illustrate the use of the Replace function, and of course, search,split functions can take advantage of regular expressions
Best practices for JavaScript regular expressions
In a word: try to streamline, not complex, conducive to reading and maintenance!

Regular expression Learning for JavaScript

Related Article

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.