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