Javascript regular expression parameters and g and and I and and gi guidelines

  • 2020-03-30 03:45:45
  • OfStack

Regularexpression = / pattern / [switch]

The 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 backward reference regular expressions is the ability to store portions of a pattern that match successfully for later use. Recall that adding parentheses to a regular expression pattern or part of a pattern causes that part of the expression to be stored in a temporary buffer. You can use non-captured metacharacters '? : ', 'the & # 63; = ', or '& # 63; ! 'to ignore the save of this part of the regular expression.

Each captured submatch is stored in terms of what is encountered from left to right in the regular expression pattern. The buffer number for storing a submatch starts at 1 and continues until a maximum of 99 subexpressions. Each buffer can be accessed using '\n', where n is a decimal digit or two that identifies a particular buffer.

One of the simplest and most useful USES of backward references is the ability to determine the position of two consecutive identical words in a text. Look at the following sentence: Is the cost of of gasoline going up? Based on what has been written, the above sentence clearly has the problem of repeating the word many times. It would be nice if there were a way to modify a sentence without looking for the repetition of each word. The following JScript regular expression USES a subexpression to do this.

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

"\b([a-z]+) \1\b" in this example, the subexpression is just each term between the parentheses. The captured expression 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 submatch, which is the second occurrence of the word matched by the additional expression. '\1' is used to specify the first child match. The word boundary element character ensures that only individual words are detected. If not, phrases such as "is issued" or "this is" will be incorrectly recognized by the expression.

In JScript expressions, 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 flag at the end of the expression (' I '). A multi-line tag specifies a potential match that may occur at either end of a newline. For VBScript, you cannot set various tags 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 submatch information to replace the same word that appears twice in a row with the same word in a literal string:

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


Related articles: