JavaScript Regular expression parameters/g with/I and/GI Guidelines _javascript tips

Source: Internet
Author: User

Regularexpression=/pattern/[switch]

This switch has three values G: Global match I: Ignore case GI: Global match + ignore case JScript Language Reference

--------------------------------------------------------------------------------

One of the most important features of a back-referencing regular expression is the ability to store portions of a successful pattern for later use. Recall that adding parentheses around a regular expression pattern or part of a pattern causes this part of the expression to be stored in a temporary buffer. You can use a non-capture meta character '?: ', '? = ', or '?! ' to ignore the preservation of this part of the regular expression.

Each captured child match is stored in the content that is encountered from left to right in the regular expression pattern. The buffer number for the storage child match starts at 1 and is numbered consecutively up to 99 subexpression. Each buffer can be accessed using ' \ n ', where n is a single or two-bit decimal number that identifies a particular buffer.

The simplest and most useful application of the post reference is to provide the ability to determine the position of two consecutive words in the text. Please see the following sentence: The cost of an gasoline going up? According to the written content, the above sentence obviously has the problem that the word repeats many times. It would be nice if there was a way to modify the sentence without having to look up the repetition of each word. The following JScript regular expression uses a subexpression to implement this functionality.

The/\b ([a-z]+) \1\b/gi equivalent VBScript expression is:

"\b ([a-z]+) \1\b" In this example, the subexpression is each item between the parentheses. The captured expression includes one or more alphabetic characters, which are specified by ' [a-z]+ '. The second part of the regular expression is a reference to the previously captured substring, the second occurrence of a word that is matched by an additional expression. ' \1 ' is used to specify the first child match. The word boundary Meta character ensures that only individual words are detected. If not, phrases such as "is issued" or "This is" are incorrectly recognized by the expression.

In a JScript expression, the global flag (' G ') that follows the regular expression indicates that the expression will be used to find as many matches as possible in the input string. Case sensitivity is specified by the case sensitivity mark (' I ') at the end of the expression. A multiline tag specifies a potential match that may appear at both ends of a newline character. For VBScript, various tags cannot be set in an expression, but must be explicitly set using the properties of the RegExp object.

Using the regular expression shown above, the following JScript code can use the child-matching information to replace the same word in a literal string with a single word that appears two consecutive times:

var ss = ' is ' The cost of ' gasoline going up?. \ n "; var re =/\b ([a-z]+) \1\b/gim; Creates a regular expression style. var rv = Ss.replace (Re, "$"); Replace two words with one word.

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.