regexp types contain information: Patterns and flags
To create a RegExp object:
Method One:
Literal form
var expression =/pattern/flags;
Flags
- G: Global mode, the pattern will be applied to all strings, rather than stopping when the first match is found
- I: Case insensitive (ignorecase)
- M: (multiline) multiline mode, which continues to find the next line when it reaches the end of a line of text
Method Two:
REGEXP (patternstring[, flagsstring]) constructors
var pattern2 = new RegExp ("\\[bc\\]at", "I");
Equivalent: var pattern2 =/\[bc\]at/i;
Note:
RegExp objects created in all literal forms in ECMASCRIPT3 share a regexp instance, and RegExp objects created by all constructors each have one regexp instance
ECMAScript5 in two different ways
RegExp Instance Properties
- Global: Boolean value, whether the G flag is set
- IgnoreCase: Boolean value. Whether the I flag is set
- Multiline: Boolean value, whether the M flag is set
- Source: String. The mode of the RegExp object. (A string in literal form, not a string in the form of a constructor)
- LastIndex: Integer that represents the starting position of the next search, from zero (test () and exec () with G set, which varies with the use of the RegExp object)
RegExp instance method
EXEC (String)
Returns the array Array,array with the index and input properties
Index property: The starting position of the match to pattern in string
Input:string
The string to match in the array[0]:string
String in array[1],array[2]....:string that matches the capturing group
Note: If the regular Expression object is set to G, then the value of the Lastindex property varies with the multiple calls of the exec ()
Test (String)
If match to pattern in string, return true, modify lastindex;
If no match is reached, return False,lastindex = 0
Inheritance method:
tolocalstring () and ToString (): returns the literal of the RegExp (string), such as: "/cat/i"
valueOf (): returns the regular expression object itself
"JS Note" 5.4 RegExp type