It is very easy and common to use regular expressions in javascript.
What is a regular expression? (Baidu encyclopedia)
Interpretation of all symbols in a regular expression
Use regular expressions in ASP
There are two ways to create a regular expression in javascript:
Syntax 1
Re =/pattern/[flags]
Syntax 2
Re = new RegExp ("pattern", ["flags"])
The pattern is the text form of a regular expression, and flags is a tag. Its meaning is as follows:
G (search for all occurrences of the pattern in full text)
I (Case Insensitive)
M (multi-row search)
Regular Expressions are commonly used in three methods. In fact, the most common method is the last method test.
Compile Method
Compile the regular expression into an internal format for faster execution.
RgExp. compile (pattern, [flags])
Parameters
Rgexp
Required. An example of a regular expression object. It can be a variable name or text.
Pattern
Required. String expression, which contains the regular expression pattern to be compiled.
Flags
Optional. It can be used in combination. The available labels include:
G (global search for all existing pattern)
I (ignore events)
M (multi-row search)
Description
The compile method converts pattern to an internal format to perform faster. For example, this allows you to use regular expressions more effectively in a loop. When the same expression is used repeatedly, the compiled regular expression accelerates execution. However, if the regular expression changes, this compilation is useless.
Example
The following example illustrates the usage of the compile method:
Function CompileDemo () {var rs; var s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp" // create a regular expression for only uppercase letters.
Var r = new RegExp ("[A-Z]", "g ");
Var a1 = s. match (r) // search for matching.
// Compile the regular expression only for lowercase letters.
R. compile ("[a-z]", "g ");
Var a2 = s. match (r) // search for matching.
Return (a1 + "\ n" + a2;
}
Exec Method
Run the search in the string in regular expression mode and return an array containing the search result.
RgExp.exe c (str)
Parameters
RgExp
Required. A regular expression object that contains the regular expression mode and available flag.
Str
Required. The String object or String text to be searched.
Description
If the exec method does not find a match, it returns null. If it finds a match, the exec method returns an array and updates the attributes of the global RegExp object to reflect the matching result. The 0 element of the array contains the complete match, and the 1st to nelement contains any child match in the match. This is equivalent to the match method without setting the global flag.
If a global flag is set for the regular expression, exec searches for it at the position indicated by the value of lastIndex. If the global flag is not set, exec ignores the value of lastIndex and searches from the starting position of the string.
The array returned by the exec method has three attributes: input, index, and lastIndex. The Input attribute contains the entire searched string. The Index attribute contains the position of the matched substring in the searched string. The LastIndex attribute contains the next position of the last character in the match.
Example
The following example illustrates the usage of the exec method:
Function RegExpTest () {var ver = Number (ScriptEngineMajorVersion () + "." + ScriptEngineMinorVersion () if (ver >=5.5) {// test the JScript version.
Var src = "The rain in Spain falls mainly in the plain .";
Var re =/\ w +/g; // create the regular expression mode.
Var arr;
While (arr = re.exe c (src ))! = Null)
Document. write (arr. index + "-" + arr. lastIndex + "\ t" + arr );
}
Else {
Alert ("use the updated version of JScript ");
}
}
Test Method
Returns a Boolean value indicating whether the pattern exists in the searched string.
Rgexp. test (str)
Parameters
Rgexp
Required. A regular expression object that contains the regular expression mode or available flag.
Str
Required. The string to be searched.
Description
The test method checks whether a mode exists in the string. If yes, true is returned. Otherwise, false is returned.
The attributes of the global RegExp object cannot be modified by the test method.
Example
The following example illustrates the usage of the test method:
Function TestDemo (re, s) {var s1; // declare the variable.
// Check whether a regular expression exists in the string.
If (re. test (s) // test whether the object exists.
S1 = "contains"; // s inclusion mode.
Else
S1 = "does not contain"; // s does not contain the mode.
Return ("'" + s + "'" + s1 + "'" + re. source + "'"); // return a string.
}
From The 0th Space