Expressions

Regex expressions

11min

By combining regular (regex) expressions in your JSONata expressions in

, you can create complex patterns to match specific strings of text. Regex expressions are a sequence of characters that define a search pattern. It is powerful when used in text processing to find, replace, or validate strings of text. Some common uses of regex include:

  • Validating email addresses or phone numbers
  • Extracting specific information from a text file or document
  • Reformatting data to match a specific pattern or structure

Examples and code snippets

The JSONata + regex examples below create validation for text-fields in a form.

Phone number validation

Expected results: Between 9 and 13 numbers with no spaces, can include a symbol for dialing code, e.g., +271234556789.

Valid phone number
Valid phone number

Invalid phone number
Invalid phone number

YAML


Email validation

Expected result: [email protected]

Valid email address
Valid email address

Invalid email address
Invalid email address

YAML


Credit card validation

Expected result: Typically 13-16 digits, with spaces or dashes optional, and includes checks for Visa, MasterCard, American Express, and Discover. E.g. 1111-1111-1111-1111 or 1111 1111 1111 1111.

Valid  credit card number
Valid credit card number

Invalid  credit card number
Invalid credit card number

YAML


ZIP/Postal code (US) validation

Expected result: 5-digit codes, e.g. 10036.

Valid ZIP code
Valid ZIP code

Invalid ZIP code
Invalid ZIP code

YAML


Social Security Number (US) validation

Expected result: XXX-XX-XXXX

Valid Social Security number
Valid Social Security number

Invalid Social Security number
Invalid Social Security number

YAML


National Insurance Number (UK) validation

Expected result: AA123456C

Valid National Insurance number
Valid National Insurance number

Invalid National Insurance number
Invalid National Insurance number

YAML


US Date (DD/MM/YYYY) validation

Expected result: DD/MM/YYYY, e.g. 23/07/2024.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Date (MM/DD/YYYY) validation

Expected result: MM/DD/YYYY, e.g. 03/28/2023.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Date (DD Month YYYY) validation

Expected result: DD Month YYYY, e.g. 25 July 2024.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Date (yyyy/mm/dd) validation

Expected result: yyyy/mm/dd, e.g. 2024/08/30.

Valid date format
Valid date format

Invalid date format
Invalid date format

YAML


Decimal validation

Expected result: 1234,00

Valid decimal number
Valid decimal number

Invalid decimal number
Invalid decimal number

YAML


Time (H:MM AM/PM) validation

Expected result: H:MM AM/PM e.g. 12:15 AM or 08:45 PM

Valid time format
Valid time format

Invalid time format
Invalid time format

YAML


Time (MM:SS / or HH:MM) validation

Expected result: MM:SS / or HH:MM, e.g. 08:10.

Valid time format
Valid time format

Invalid time format
Invalid time format

YAML


Time in 24-hour format

Expected result: 01:00

Valid time format
Valid time format

Invalid time format

YAML


URL validation

Expected result: example.com or http://example.com

Valid URL
Valid URL

Invalid URL
Invalid URL

YAML


ISBN validation

Expected result: 978-1-4302-1998-9

Valid ISBN
Valid ISBN

Invalid ISBN
Invalid ISBN

YAML


Strict Alpha Numeric validation

Expected result: JohnSmith

Valid string -no spaces
Valid string -no spaces

Invalid string -no spaces
Invalid string -no spaces

YAML


Alpha Numeric with spaces allowed validation

Expected result: John Smith

Valid string with spaces
Valid string with spaces

Invalid string with spaces
Invalid string with spaces

YAML


validation of numbers and Spaces only

Expected result: 56575 76 6

Valid numbers with spaces
Valid numbers with spaces

Invalid numbers with spaces
Invalid numbers with spaces

YAML