Skip to main content

$contains

$contains(string?, pattern) -> boolean
Compact type signature<s-(sf):b>

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

Browse categories

Source: JSONata

Documentation

Returns true if str is matched by pattern, otherwise it returns false.

The pattern parameter can either be a string or a regular expression (regex). If it is a string, the function returns true if the characters within pattern are contained contiguously within str. If it is a regex, the function will return true if the regex matches the contents of str.

Examples

Basic usage

  • $contains("abracadabra", "bra") => true
  • $contains("abracadabra", /a.*a/) => true
  • $contains("abracadabra", /ar.*a/) => false
  • $contains("Hello World", /wo/) => false
  • $contains("Hello World", /wo/i) => true
  • Phone[$contains(number, /^077/)] => { "type": "mobile", "number": "077 7700 1234" }

Detect a token in free text

Input

This example uses the free-text-notes example input. The expression reads the second note and searches it for the token.

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

$contains($[1], "DX-001")

Result

true