.match(pattern)

Description

match executes a search for a match between a regular expression and the string, returning an array containing the results of that search. It works identically to JavaScript's String.match() method.

Parameters

match needs one parameter:

  • pattern: A string containing the regular expression pattern and optional flags (e.g. "/pattern/flags")

Usage On Strings

str = "The rain in Spain"

result = str.match("/ain/g")
log result
// ["ain", "ain"]
// Returns all matches with global flag

result = "test@example.com".match("/^[^@]+@[^@]+\.[^@]+$/")
log result
// ["test@example.com"]
// Email validation example

result = "Hello World".match("/(\w+)\s(\w+)/")
log result
// ["Hello World", "Hello", "World"]
// Capturing groups example

result = "ABC123".match("/[0-9]+/")
log result
// ["123"]
// Matching numbers

// Common flags:
// g - Global search (find all matches rather than stopping after the first match)
// i - Case-insensitive search
// m - Multiline search
result = "Hello hello HELLO".match("/hello/gi")
log result
// ["Hello", "hello", "HELLO"]

Last updated

Was this helpful?