Search

6/04/2010

regexp.exec, string.match

via javascript: the definitive guide


string.match

  • if g flag was used: returns an array with the matches

  • if g was not used: an array with the match and the parenthesized subexpressions



'123-456-7890'.match(/\d{3}/g); // Returns ['123', '456’, ‘789’]
'123-456-7890'.match(/(\d{3})-(\d{3})/); // Returns ['123-456', '123', '456']

regexp.exec
Note that exec( ) always includes full details of every match in the array it returns, whether or not regexp is a global pattern. This is where exec( ) differs from String.match( ), which returns much less information when used with global patterns. Calling the exec( ) method repeatedly in a loop is the only way to obtain complete pattern-matching information for a global pattern.

沒有留言: