Home  >  Article  >  Web Front-end  >  Detailed explanation of the method of obtaining group content using JS regular expression_javascript skills

Detailed explanation of the method of obtaining group content using JS regular expression_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:441516browse

Support multiple matching methods:

Copy code The code is as follows:

var testStr = "now test001 test002";
var re = /test(d )/ig;
var r = "";
while(r = re.exec(testStr)) {
alert(r[0] " " r[1] );
}

You can also use testStr.match(re), but in this case you cannot have the g option, and you can only get the first match.

Another note:

Properties and methods of regular expression objects:
Predefined regular expressions have the following static properties: input, multiline, lastMatch, lastParen, leftContext,

rightContext and $1 to $9. Among them, input and multiline can be preset. The values ​​of other attributes are determined according to

after executing the exec or test method.

Different conditions are assigned different values. Many properties have both long and short (perl-style) names, and both names refer to the same value. (JavaScript simulates perl’s regular expression)

Attributes of the regular expression object:
Attribute meaning
$1...$9 If they (they) exist, it is the matched substring
$_ See input
$* See multiline
$& See lastMatch
$ See lastParen
$` See leftContext
$''  See rightContext
constructor Create a special function prototype of an object
global Whether it is in the entire string Match (bool type)
ignoreCase Whether to ignore case when matching (bool type)
input The matched string
lastIndex The last matched index
lastParen The last substring enclosed in parentheses
leftContext  The most recent matched substring to the left
multiline   Whether to perform multi-line matching (bool type)
prototype   Allow additional attributes to the object
rightContext   The most recent matched substring to the right
source   Regular expression Formula pattern
lastIndex  The index of the last match

Methods of regular expression objects:
Method meaning
compile  This should refer to redefining the content of the regular expression
exec  To perform a search, you can use the while method to search multiple times for
test   Matches
toSource Returns the literal representation of a specific object, whose value can be used to create a new object. Obtained by overloading the Object.toSource method.
toString    Returns the string of a specific object. Obtained by overloading the Object.toString method.
valueOf    Returns the original value of a specific object. Overload the Object.valueOf method to get

Example:

Copy the code The code is as follows:



will output "Smith, John"
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn