The meaning of JavaScript regular expressions/g and/I and/GI

Source: Internet
Author: User

Original: JavaScript regular expressions/g and/I and/gi meanings

regularexpression=/pattern/[Switch]

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

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

Back-to-reference regular expressions one of the most important features is the ability to store a portion of a pattern that matches successfully for later use. Recall that adding parentheses around either a regular expression pattern or a partial pattern causes this part of the expression to be stored in a temporary buffer. You can use the non-capturing metacharacters '?: ', '? = ', or '?! ' to ignore the preservation of this part of the regular expression.

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

One of the simplest and most useful applications for a back reference is the ability to determine the position of two consecutive words in a text. Take a look at the following sentence: Is the cost of the gasoline going up? According to the written content, the above sentence obviously has the word repeated repeatedly question. It would be nice if there was a way to modify the sentence without finding the repetition of each word. The following JScript regular expression can implement this function using a subexpression.

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

The equivalent VBScript expression is:

"\b ([a-z]+) \1\b"

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

In a JScript expression, the global flag (' G ') after 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 tag (' 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, you cannot set various tokens in an expression, but you must explicitly set them using the properties of the RegExp object.

Using the regular expression shown above, the following JScript code can use the sub-match information to replace the same word with the same word in a single literal string with a continuous occurrence of two occurrences:

var var // creates a regular expression style.
var //

The meaning of JavaScript regular expressions/g and/I and/GI

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.