Skip to main content

$match

$match(string?, pattern, limit?) -> array<object>
Compact type signature<s-f<s:o>n?:a<o>>

If string is omitted, the current context value is used.

Browse categories

Source: JSONata

Documentation

Applies the str string to the pattern regular expression and returns an array of objects, with each object containing information about each occurrence of a match withing str.

The object contains the following fields:

  • match - the substring that was matched by the regex.
  • index - the offset (starting at zero) within str of this match.
  • groups - if the regex contains capturing groups (parentheses), this contains an array of strings representing each captured group.

It is an error if str is not a string.

Examples

Basic usage

$match("ababbabbcc",/a(b+)/) =>

[
{
"match": "ab",
"index": 0,
"groups": ["b"]
},
{
"match": "abb",
"index": 2,
"groups": ["bb"]
},
{
"match": "abb",
"index": 5,
"groups": ["bb" ]
}
]

Extract a code-shaped token (with capturing groups)

Input

This example uses the free-text-notes example input. The expression reads the second note and extracts a code-shaped token with one capture group.

Example input

JSON
[
" follow-up recommended in 2 weeks ",
"reports intermittent headache; DX-001 noted",
"plan PLAN-100: confirm eligibility for mbr-3001",
"lab LAB-1002 flagged as HIGH; repeat test",
"pat-0001: patient prefers morning appointments",
"normalize multiple spaces"
]

Expression

$match($[1], /DX-(\d{3})/)

Result

{
"match": "DX-001",
"index": 31,
"groups": ["001"]
}