Javascript Regular Expression parameters/g and/I and/gi User Guide, regular expression gi

Source: Internet
Author: User

Javascript Regular Expression parameters/g and/I and/gi User Guide, regular expression gi

Regularexpression =/pattern/[switch]

This switch has three values: g: Global match I: case-insensitive gi: Global match + case-insensitive JScript Language Reference

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

One of the most important features of backward reference regular expressions is to store a part of the matched pattern for future use. Recall that adding parentheses on both sides of a regular expression mode or partial mode will cause this expression to be stored in a temporary buffer. Can I use non-captured metacharacters '? :','? = ', Or '?! 'To ignore the storage of this part of the regular expression.

Each captured sub-match is stored in the content from left to right in the regular expression mode. The buffer number that stores the sub-match starts from 1 and ranges from consecutive numbers to a maximum of 99 subexpressions. Each buffer zone can be accessed using '\ n', where n is one or two decimal digits that identify a specific buffer zone.

Backward reference is the simplest and most useful application, which provides the ability to determine the continuous occurrence of two identical words in a text. See the following sentence: Is the cost of gasoline going up? According to the content, the above sentence is obviously prone to repeated words. If there is one way to modify the sentence without looking for the repetition of each word. The following JScript Regular Expression uses a subexpression to implement this function.

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

"\ B ([a-z] +) \ 1 \ B" in this example, the subexpression is each item between parentheses. The captured expression contains one or more letter characters, that is, specified by '[a-z] +. The second part of the regular expression is a reference to the previously captured sub-match, that is, the second occurrence word matched by the append expression. '\ 1' is used to specify the first child match. The word boundary character ensures that only individual words are detected. Otherwise, phrases such as "is issued" or "this is" are incorrectly recognized by this expression.

In a JScript expression, the global sign ('G') after the regular expression indicates that the expression will be used to search for as many matches as possible in the input string. The case sensitivity is specified by the case sensitivity mark ('I') at the end of the expression. Multiline flag specifies a potential match that may appear at both ends of the line break. For VBScript, you cannot set various tags in expressions, but you must use the RegExp object attribute to explicitly set the tags.

Using the regular expression shown above, the following JScript code can use sub-matching information to replace two identical words with one in a text string:

Var ss = "Is the cost of gasoline going up ?. \ N "; var re =/\ B ([a-z] +) \ 1 \ B/gim; // create a regular expression style. var rv = ss. replace (re, "$1"); // replace two words with one word.


What does $ in JavaScript regular expression syntax mean?

Javascript Regular Expressions/g and/I and/gi meanings regularexpression =/pattern/[switch]
This switch has three values.
G: Global match
I: case insensitive
Gi: The above combination // is like "". The "_" in the middle is the regular expression pattern, and the "g" in the name is the option. The method for declaring the matching is actually changing "_" to "$, personal opinions for your reference.

How do I use regular expressions in javascript ??

<Script type = "text/javascript">
Var reg =/^ \ d {5} $ /;
Var str = '20140901 ';
Var result;

Result = reg. test (str); // test: Check if str matches regular ^ \ d {5 }$, return true or false

// Result = reg.exe c (str); // search: The str character matches the regular character ^ \ d {5} $. If no value is found, null is returned.

</Script>

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.