Skip to main content

$regex

The $regex operator checks if the first string matches the regular expression pattern provided in the second string.

Input

  • An array containing exactly two string values: the input string and the regex pattern

Output

  • true if the first string matches the regex pattern
  • false otherwise

Examples

Matching Pattern

{
  "$regex": [
    "hello@example.com",
    "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
  ]
}
Result: true

Non-matching Pattern

{
  "$regex": [
    "not-an-email",
    "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
  ]
}
Result: false

Field Value

{
  "$regex": [
    {
      "$input": "phone_number"
    },
    "^\\+?[1-9]\\d{1,14}$"
  ]
}
Result: true if the phone_number field matches the international phone number pattern, otherwise false