String.match
match($regexPattern, $groupNames = '') -> string
Description
Apply a regex pattern and return the first match.
Returns an empty string ''
if there is no match.
If there is a match, it will return the matched substring.
'abc 123'.match(r'abc \d+') //= 'abc 123' 'abc 123'.match(r'xyz \d+') //= '' // Ignore-case flag 'abc 123'.match(r'ABC'i) //= 'abc'
Capture Groups
If the regex pattern contains capture groups defined by parens (...)
, it will return a Map containing the submatch for each group.
Each group of is assigned a number according to where its left paren (
appears in the pattern.
If $groupNames
is given a pipe-delimited string of names, those will be used instead of numbers.
The key full
will contain the full outer match.
The key indexOf
contains the index position of each group.
// Capture groups 'Time: 08:23'.match(r'(\d+):(\d+)') // { // full: 'Time: 08:23', // 1: '08' // 2: '23' // indexOf: { 1: 7, 2: 10 }, // } // Capture group names 'Time: 08:23'.match(r'(\d+):(\d+)', 'hour|minute') // { // full: 'Time: 08:23', // hour: '08' // minute: '23' // indexOf: { hour: 7, minute: 10 }, // }