Detailed explanation of how to obtain group content using JS Regular Expressions

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.