Multiple matching modes are supported:
Copy codeThe Code is as follows:
Var testStr = "now test001 test002 ";
Var re =/test (\ d +)/ig;
Var r = "";
While (r = re.exe c (testStr )){
Alert (r [0] + "" + r [1]);
}
In addition, testStr. match (re) can be used, but in this case, the g option cannot be used, and only the first match can be obtained.
Note:
Attributes and methods of a regular expression object:
Predefined regular expressions have the following static attributes: input, multiline, lastMatch, lastParen, leftContext,
RightContext and $1 to $9. Input and multiline can be pre-set. The value of other attributes is
Different conditions are assigned with different values. Many attributes have both long and short (perl style) names, and these two names point to the same value. (JavaScript simulates the Regular Expression of perl)
Attributes of a regular expression object:
Attribute meaning
$1... $9 if it exists, it is a matched substring.
$ _ See input
$ * See multiline
$ & See lastMatch
$ + See lastParen
$ 'See leftContext
$ ''See rightContext
Constructor creates a special function prototype of an object.
Whether global matches the entire string (bool type)
Whether to ignore case sensitivity when matching ignoreCase (bool type)
Input matched string
LastIndex the last matched Index
The substring enclosed in the last square brackets of lastParen.
The last time leftContext matches a substring with the left
Whether multiline matches multiple rows (bool type)
Prototype allows attributes to be attached to an object.
RightContext: The last matched substring with the right
Source regular expression mode
LastIndex the last matched Index
Method of the regular expression object:
Meaning
Compile, which should be used to redefine the content of a regular expression.
Exec performs search. You can use the while method to search multiple times.
Test
ToSource returns the definition of a specific object (literal representing). Its value can be used to create a new object. This is obtained by reloading the Object. toSource method.
ToString returns the string of a specific object. The result is obtained by reloading the Object. toString method.
ValueOf returns the original value of a specific object. Obtain the value of the Object. valueOf method.
Example:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Var myReg =/(w +) s (w + )/;
Var str = "John Smith ";
Var newstr = str. replace (myReg, "$2, $1 ");
Document. write (newstr );
</Script>
Output "Smith, John"