Version: v0.8.1 - Beta.  We welcome contributors & feedback.

Regexes

A regex (regular expression) is a way to search a string for a given pattern.

The syntax can look confusing at first, but it is very powerful and flexible.

Regex strings are prefixed with a lowercase rx.

$regex = rx'pattern'

Examples

Many string methods take regexes as an argument.

$pattern = rx'(orange|red|yellow)'
'large red hoodie'.contains($pattern)
//= true

// Match one or more digits
'The number is: 123'.match(rx'\d+')
//= '123'

// Match one or more word characters (a-z)
'abc 123'.match(rx'\w+')
//= 'abc'

// Match one or more characters of
// lowercase 'a' through 'f'
'ID: 123-456-edca'.match(rx'[a-f]+')
//= 'edca'

Case Matching

By default, the pattern must match the case exactly.

You can append an i after the regex string to make the pattern case-insensitive.

'Blue Green'.match(rx'(red|blue)'i)
//= 'Blue'                       ^

Dynamic Regexes

If necessary, you can create a regex pattern from a regular string.

However, you will need to double-backslash any special characters.

$rx = Regex('abc \\d+', 'i')

'ABC 123'.match($rx)
//= 'ABC 123'

See Also