Reverse references in JScript and reverse references in jscript
One of the most important features of regular expressions is the ability to store part of the matching pattern for future reuse. You may remember that adding parentheses on both sides of the regular expression mode or mode will cause some of the expressions to be stored in the temporary buffer. Can I use non-capturing metacharacters? :,? = Or ?! To rewrite the capture.
Each captured sub-match is stored in the order they appear from left to right in the regular expression mode. The buffer number starts from 1 and can store a maximum of 99 captured subexpressions. You can use \NTo access each buffer zone.NIs one or two decimal digits that identify a specific buffer.
One of the simplest and most useful applications of reverse reference is to provide the ability to find matching items of two identical adjacent words in text. The following sentence is used as an example:
Is is the cost of of gasoline going up up?
The above sentence clearly contains multiple repeated words. It would be nice to design a way to locate the sentence without having to look for repeated occurrences of each word. The following regular expressions use a single subexpression to achieve this:
/\b([a-z]+) \1\b/gi
The captured expression, as specified by [a-z] +, contains one or more letters. The second part of a regular expression is a reference to the previously captured sub-matching items. That is, the second matching item of a word is matched by a bracket expression. \ 1 specifies the first child match. Ensure that only the entire word is detected. Otherwise, phrases such as "is issued" or "this is" cannot be correctly recognized by this expression.
The global mark (g) after the regular expression indicates that the expression is applied to the input string to find as many matches as possible. The end of the expression is case insensitive (I. A potential match may occur between two sides of a line break when multiple lines are marked.
Using the regular expression above, the following code can use the sub-match item information to replace the matching items of two consecutive identical words in the text string with a single match item of the same word:
var ss = "Is is the cost of of gasoline going up up?.\n";var re = /\b([a-z]+) \1\b/gim; //Create regular expression pattern.var rv = ss.replace(re,"$1"); //Replace two occurrences with one.
InReplaceUse in Method$1REFERENCE The First saved sub-match. If you have multiple child matches, you can use$2,$3And so on.
A reverse reference can also break down a common resource identifier (URI) into its components. Suppose you want to break down the following URI into protocol (ftp, http, etc.), domain address, and page/path:
http://msdn.microsoft.com:80/scripting/default.htm
The following regular expression provides this function:
/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/
The first parentheses subexpression captures the protocol section of the Web address. This subexpression matches any word before the colon and two forward slashes. The second parentheses subexpression captures the domain address of an address. The subexpression matches one or more characters other than/or. The third parentheses subexpression captures the port number (if specified ). This subexpression matches zero or multiple numbers after the colon. This subexpression can be repeated only once. Finally, the fourth parentheses subexpression captures the path and/or page information specified by the Web address. This subexpression can match any character sequence that does not contain # or space characters.
JavaScript Regular Expression matching, digits in reverse reference
In fact, the method is very simple
Use an escape character '\' to solve the problem.
To use group 1 as '$1 \ 0'
To use group 10 as '$10'
Because the number is still a number after it is escaped, you don't have to worry about any sequelae. It is just used to separate numbers.
What is reverse reference of a regular expression? What is its role?
For example, if I want to find two words that are connected together, abcabc will be used.
(Abc) \ 1
\ 1 indicates reverse reference, and \ n indicates the nth capture of the same regular expression.
Reverse references are still useful for replacement. For example, I want to replace the entire sentence with the first word.
This is a test
Replace ("(\ w +). *", "\ 1 ")