Section 24.170. String.match( ): find one or more regular-expression matches


24.170. String.match( ): find one or more regular-expression matches

ECMAScript v3

24.170.1. Synopsis

string.match(regexp)

24.170.1.1. Arguments

regexp

A RegExp object that specifies the pattern to be matched. If this argument is not a RegExp, it is first converted to one by passing it to the RegExp( ) constructor.

24.170.1.2. Returns

An array containing the results of the match. The contents of the array depend on whether regexp has the global "g" attribute set. Details on this return value are given in the Description.

24.170.2. Description

match( ) searches string for one or more matches of regexp. The behavior of this method depends significantly on whether regexp has the "g" attribute or not (see Chapter 11 for full details on regular expressions).

If regexp does not have the "g" attribute, match( ) searches string for a single match. If no match is found, match( ) returns null. Otherwise, it returns an array containing information about the match that it found. Element 0 of the array contains the matched text. The remaining elements contain the text that matches any parenthesized subexpressions within the regular expression. In addition to these normal array elements, the returned array also has two object properties. The index property of the array specifies the character position within string of the start of the matched text. Also, the input property of the returned array is a reference to string itself.

If regexp has the "g" flag, match( ) does a global search, searching string for all matching substrings. It returns null if no match is found, and it returns an array if one or more matches are found. The contents of this returned array are quite different for global matches, however. In this case, the array elements contain each of the matched substrings within string. The returned array does not have index or input properties in this case. Note that for global matches, match( ) does not provide information about parenthesized subexpressions, nor does it specify where within string each match occurred. If you need to obtain this information for a global search, you can use RegExp.exec( ).

24.170.3. Example

The following global match finds all numbers within a string:

 "1 plus 2 equals 3".match(/\d+/g)  // Returns ["1", "2", "3"] 

The following nonglobal match uses a more complex regular expression with several parenthesized subexpressions. It matches a URL, and its subexpressions match the protocol, host, and path portions of the URL:

 var url = /(\w+):\/\/([\w.]+)\/(\S*)/; var text = "Visit my home page at http://www.isp.com/~david"; var result = text.match(url); if (result != null) {     var fullurl = result[0];   // Contains "http://www.isp.com/~david"     var protocol = result[1];  // Contains "http"     var host = result[2];      // Contains "www.isp.com"     var path = result[3];      // Contains "~david" } 

24.170.4. See Also

RegExp, RegExp.exec( ), RegExp.test( ), String.replace( ), String.search( ); Chapter 11




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net