Reverse references in JScript and using reverse references

Source: Internet
Author: User

One of the most important features of regular expressions is the ability to store part of a matching pattern for later reuse. As you may recall, if you enclose a regular expression pattern or part of a pattern with parentheses, it causes the part of the expression to be stored in a temporary buffer. Can I use non-capturing metacharacters?:,? = or?! To override the capture.

Each captured sub-match is stored in the order in which they appear from left to right in the regular expression pattern. The buffer number starts at 1 and stores up to 99 captured sub-expressions. You can use \n to access each buffer, where n is one or two decimal digits that identify a particular buffer.

One of the simplest and most useful applications for reverse referencing is the ability to provide a match to find two identical adjacent words in text. Take the following sentence as an example:

Is are the cost of gasoline going up?

The above sentence obviously has multiple repeating words. If you can design a method to locate the sentence without having to find the repetition of each word, how good is it. The following regular expression uses a single sub-expression to accomplish this:

/\b ([a-z]+) \1\b/gi

The captured expression, as specified by [a-z]+, includes one or more letters. The second part of the regular expression is a reference to a previously captured sub-match, that is, the second occurrence of the word is exactly matched by a parenthesis expression. \1 Specifies the first child match. Character Boundary metacharacters ensure that only the entire word is detected. Otherwise, phrases such as "is issued" or "This is" will not be recognized correctly by this expression.

The global Tag (g) Following the regular expression indicates that the expression is applied to as many matches as can be found in the input string. The case-insensitive (i) mark at the end of the expression specifies case insensitive. A multiline tag specifies that a potential match may occur on either side of a line break.

Using the preceding regular expression, the following code can use the sub-match information to replace the occurrences of two consecutive identical words in a text string with a single occurrence of the same word:

var ss = "Is was the cost of the gasoline going up?. \ n "; var re =/\b ([a-z]+) \1\b/gim;       Create Regular expression Pattern.var RV = Ss.replace (Re, "$");   Replace the occurrences with one.

Within the replace method, Use the reference to the first saved child match. If you have multiple sub-matches, you will refer to them in turn by using $ $, $ $, and so on.

A reverse reference can also decompose a generic resource indicator (URI) into its components. Assume that you want to break down the following URIs into protocols (FTP, HTTP, and so on), domain addresses, and page/path:

Http://msdn.microsoft.com:80/scripting/default.htm

The following regular expression provides this functionality:

/(\w+): \/\/([^/:]+) (: \d*)? ([^# ]*)/

The first parenthesis subexpression captures the protocol portion of the WEB address. The subexpression matches any word in front of a colon and two forward slashes. The second parenthesis subexpression captures the domain address portion of the address. Sub-expression matches/or: one or more characters apart. The third parenthesis subexpression captures the port number (if specified). The subexpression matches 0 or more digits following the colon. This subexpression can be repeated only once. Finally, a fourth parenthesis subexpression captures the path and/or page information specified by the WEB address. The subexpression can match any sequence of characters that does not include a # or a space character.

Reverse references in JScript and using reverse references

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.