About Reverse References
Copy 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 passed
Matchreg (Reg, str);
Do not record subexpression matches by (?:p Attern) (in this case, Andrew)
So \1 the subexpression that matched the content failed.
Note: The emphasis here is on what the subexpression matches, not the subexpression itself
Reg =/(?: [a-za-z]{0,6}) \1/;
Test does not pass
Matchreg (Reg, str);
About the definition of a subexpression
var parse_number =/^-?\d+ (?: \. \d*)? (?: e[+\-]?\d+)? $/i;
This is a regular expression that parses a number, where the subexpression has a (?: \. \d*) and (?: e[+\-]?\d+)
\. and \-, respectively, are the escape expressions for. and-
Review it by the way. Represents a character other than a line break
-Generally used [a-za-z0-9] to represent the range of matches
If not: for example (\.\d*) then, in order of precedence, the relationship of the reverse reference is as follows
\1--> (\.\d*)
\2--> (e[+\-]?\d+)
If there are more words, and so on \3 \4 \5 ....
Again, the reference is to the content of the subexpression matching, is the specific text
About forward pre-investigation
Copy Code code as follows:
var reg =/I like (? =shanghai)/;
var str = ' I like Shanghai ';
Matchreg (Reg, str); Test passed
str = ' I like Beijing ';
Matchreg (Reg, str); Not through
Run down the above code, immediately understand what is a forward check, such as the above example, through the form of (? =pattern), the regular expression predicts that the following content is not in line with the requirements, if it is the smooth match.
Relatively, (?!) The use of =pattern) is exactly the opposite of (? =pattern), and it is no longer stated.
On greedy and non-greedy matching patterns
Copy Code code as follows:
Greedy
var reg =/\d{1,}/;
var str = ' 1999 ';
Matchreg (Reg, str); Result[0] For 1999 can match more than match
Not 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 "greed" and "not greedy"
This shows that the non-greedy matching pattern, the general situation is greedy
About the results returned by the EXEC function of the RegExp object
Copy Code code as follows:
What is the result of the return?
Matchreg function Console.dir (result) can explain the problem, firebug a look at the know
About the Replace function for 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 be concise, not complex, conducive to read and maintain!