Home > Article > Web Front-end > Guide to the use of javascript regular expression parameters /g, /i and /gi_javascript skills
regularexpression=/pattern/[switch]
This switch has three values g: Global match i: Ignore case gi: Global match Ignore case JScript Language Reference
-------------------------------------------------- ----------------------------------
Back references One of the most important features of regular expressions is the ability to store part of a successfully matched pattern for later use. Recall that adding parentheses around a regular expression pattern or part of a pattern will cause that part of the expression to be stored in a temporary buffer. You can use the non-capturing metacharacters '?:', '?=', or '?!' to ignore saving this part of the regular expression.
Each captured submatch is stored as encountered from left to right in the regular expression pattern. The buffers in which submatches are stored are numbered starting from 1 and numbered consecutively up to a maximum of 99 subexpressions. Each buffer can be accessed using 'n', where n is a one- or two-digit decimal number that identifies a specific buffer.
One of the simplest and most useful applications of backreferences is the ability to determine the position in text where two identical words occur consecutively. Take a look at the following sentence: Is the cost of gasoline going up up? Based on what is written, the above sentence obviously has the problem of multiple repetitions of words. It would be great if there was a way to modify this sentence without looking for duplicates of every word. The following JScript regular expression uses a subexpression to achieve this functionality.
/b([a-z] ) The equivalent VBScript expression of 1b/gi is:
"b([a-z] ) 1b" In this example, the subexpression is each term between the parentheses. The captured expression consists of one or more alphabetic characters, as specified by '[a-z] '. The second part of the regular expression is a reference to the previously captured submatch, which is the second occurrence of the word matched by the appended expression. '1' is used to specify the first submatch. Word boundary metacharacters ensure that only individual words are detected. If this were not done, phrases such as "is issued" or "this is" would be incorrectly recognized by the expression.
In a JScript expression, the global flag ('g') after a 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 a case sensitivity marker ('i') at the end of the expression. Multiline tags specify potential matches that may occur on either side of a newline character. For VBScript, various tags cannot be set in expressions but must be set explicitly using the properties of the RegExp object.
Using the regular expression shown above, the following JScript code can use submatch information to replace two consecutive occurrences of the same word with the same word in a literal string:
var ss = "Is is the cost of gasoline going up up?.n"; var re = /b([a-z] ) 1b/gim; //Create regular expression style.var rv = ss.replace (re,"$1"); //Replace two words with one word.