Version: v0.6.1 - Beta.  We welcome contributors & feedback.  THanks!

String.match

match($regexPattern)

Description

Apply a regex pattern and return the first match.

Returns false if there is no match.

If there is a match, it returns a List where the first item is the entire match.

If there are capture groups (parenthesis) in the pattern, the returned list will also contain each of the captured matches in order. (i.e. the first capture group will be at position 1, etc.)

Call the flags method on the regex string to add modifier flags.

'abc 123'.match(r'abc \d+');
//= ['abc 123']

'abc 123'.match(r'xyz \d+');
//= false

// Ignore-case flag
'Abc'.match(r'abc'.flags('i'));
//= ['Abc']

// Entire match, followed by capture groups
'abc 123 456'.match(r'abc (\d+) (\d+)');
//= ['abc 123 456', '123', '456']

See Also