Skip to content

Regular Expression Library

Tested patterns for checking and extracting common values: document numbers, amounts, contact details, addresses and dates, plus helpers that pull values out of OCR text. KnowledgeLake has two regular expression engines, and they take patterns in different forms. Read "Where patterns run" before you copy a pattern.

At a glance

  • In code (behaviors, Custom Step, Custom Event, routes, Intuitive AI scripts, gizmos): JavaScript RegExp, written as a literal such as /^\d{5}$/.
  • In configuration (Input Mask, Regex Anchor, OCR action refiner): pattern text only, no slashes.
  • Intuitive AI patterns Run in .NET with a 5 second limit.
  • Rule one Anchor full-value checks with ^ and $.

Patterns on this page

Section What it covers
Where patterns run The two engines and how to enter a pattern
Document identifiers Invoice, PO and document numbers
Amounts and accounts Amounts, bank accounts, IBAN, card number shape
People and contact details Email, phone, SSN, person names
Addresses US ZIP, Canadian postal code, state code
Dates and times US, EU and ISO dates, 12 and 24 hour times
Extracting values from text Pull amounts, emails, dates and labeled values from OCR text
Cleaning text Whitespace, tags, masking
Matching a word, not a substring containsWord with word boundaries
Quick reference Pattern text for an Input Mask or a Regex Anchor

Where patterns run

Where Engine How to enter the pattern
Custom Behaviors, search filter behaviors, Custom Step, Custom Event, routes, Intuitive AI scripts, Custom Gizmos JavaScript RegExp As a literal in code: /^\d{5}$/
Input Mask behavior (Textbox, NumericUpDown) JavaScript RegExp Pattern text only, no slashes, no flags: ^\d{5}$
Intuitive AI Regex Anchor refiner .NET Regex, 5 second limit Pattern text only. Flags: i, x, M, e. No flags means case-insensitive.
Intuitive AI OCR action, regex refiner .NET Regex, 5 second limit Pattern text only. Flags are ignored: it always runs case-insensitive.

Rules for all of them:

  1. Anchor full-value checks with ^ and $. The Input Mask behavior does not add anchors: \d{5} accepts abc12345xyz.
  2. In an Input Mask, a value that does not match marks the field invalid. An empty value is allowed.
  3. The patterns on this page use only syntax that both engines read the same way: character classes, \d, \s, \w, groups, ?, *, +, {n,m}, alternation and lookahead. In .NET, \d also matches digits from other scripts. That rarely matters for documents in Latin script.
  4. Avoid nested repetition such as (\d+)+. On a long value it can run past the time limit: 5 seconds in behaviors and Regex Anchor, 1.5 seconds in search.

A pattern in a string needs double backslashes

In a literal, /\d+/ is right. In new RegExp("..."), write "\\d+". A single backslash in a string is lost, and the pattern matches the letter d.

Document identifiers

Document identifier patterns
var invoiceNumber = /^INV-\d{4}-\d{4}$/;            // INV-2026-0001
var poNumber      = /^PO\d{6,10}$/;                 // PO123456
var documentId    = /^[A-Z]{2,4}-[A-Z0-9]{6,12}$/i; // DOC-ABC123456

var parts = /^INV-(\d{4})-(\d{4})$/.exec("INV-2026-0157");
// parts[1] = "2026", parts[2] = "0157"

Amounts and accounts

Amount and account patterns
var amountWithCents = /^\$?\d{1,3}(,\d{3})*\.\d{2}$|^\$?\d+\.\d{2}$/; // $1,234.56 or 1234.56
var usBankAccount   = /^\d{4,17}$/;
var iban            = /^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/;               // DE89370400440532013000
var cardNumberShape = /^(\d[ -]?){12,18}\d$/;                         // 13 to 19 digits, spaces or dashes

cardNumberShape checks the shape only. Do not store full card numbers in index fields.

To turn an amount into a number, use parseAmount from the JavaScript Utilities Library.

People and contact details

Contact detail patterns
var email       = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
var strictEmail = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
var usPhone     = /^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;   // (555) 123-4567, 555.123.4567, 5551234567
var intlPhone   = /^\+\d{1,3}[\s.-]?\d{6,14}$/;              // +44 2071234567
var usSsn       = /^\d{3}-?\d{2}-?\d{4}$/;                   // 123-45-6789
var personName  = /^[A-Za-z][A-Za-z\s'-]{1,49}$/;            // Mary O'Brien, Jean-Pierre

personName rejects letters with accents. Use it only where names are known to be plain ASCII.

Addresses

Address patterns
var usZip      = /^\d{5}(-\d{4})?$/;                       // 12345, 12345-6789
var caPostal   = /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/;  // K1A 0B1
var usStateKey = /^[A-Z]{2}$/;

usStateKey checks the shape only. Compare with a list of state codes for a real check.

Dates and times

Date and time patterns
var usDate  = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/; // 12/31/2026
var euDate  = /^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/; // 31/12/2026
var isoDate = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])/;    // 2026-12-31, also the start of a stored date value
var time12  = /^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$/i;            // 9:30 AM
var time24  = /^([01]\d|2[0-3]):[0-5]\d$/;                         // 14:30

These check the shape, not the calendar: 02/31/2026 passes usDate. A date field in a behavior holds an ISO string, so test it with isoDate, not usDate.

Extracting values from text

Use these in a Custom Step or an Intuitive AI script, on OCR text or a long field. Collapse whitespace first: OCR often breaks a phrase across lines.

Extraction helpers
function extractAmounts(text) {
    // Finds amounts such as 1,234.56 or 25 and returns numbers.
    var matches = String(text || "").match(/\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?/g) || [];
    var out = [];
    for (var i = 0; i < matches.length; i++) {
        out.push(parseFloat(matches[i].replace(/,/g, "")));
    }
    return out;
}
// extractAmounts("Total: $1,234.56 for 25 items") -> [1234.56, 25]

function extractEmails(text) {
    return String(text || "").match(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g) || [];
}
// extractEmails("Contact john@example.com or jane@example.org") -> ["john@example.com", "jane@example.org"]

function extractDates(text) {
    return String(text || "").match(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}\b|\b\d{4}-\d{1,2}-\d{1,2}\b/g) || [];
}
// extractDates("Invoice dated 03/15/2026, due 2026-04-15") -> ["03/15/2026", "2026-04-15"]

function extractLabeledValue(text, label) {
    // Value after "Label:" up to the end of the line. label must not contain regex characters.
    var m = new RegExp(label + "\\s*:\\s*([^\\r\\n]+)", "i").exec(String(text || ""));
    return m ? m[1].replace(/\s+$/, "") : "";
}
// extractLabeledValue("Invoice No: 4417\nDate: 2026-10-01", "Invoice No") -> "4417"

Cleaning text

Cleaning helpers
function normalizeWhitespace(text) {
    return String(text || "").replace(/\s+/g, " ").replace(/^\s+|\s+$/g, "");
}
// normalizeWhitespace("  Hello   World  ") -> "Hello World"

function stripTags(text) {
    return String(text || "").replace(/<[^>]*>/g, "");
}
// stripTags("<p>Hello <b>World</b></p>") -> "Hello World"

function maskSsn(ssn) {
    return String(ssn || "").replace(/^(\d{3})-?(\d{2})-?(\d{4})$/, "***-**-$3");
}
// maskSsn("123-45-6789") -> "***-**-6789"

function maskCard(card) {
    var digits = String(card || "").replace(/\D/g, "");
    return "**** **** **** " + digits.slice(-4);
}
// maskCard("4111 1111 1111 1111") -> "**** **** **** 1111"

stripTags is for display text only. It is not a safe way to clean untrusted HTML.

Matching a word, not a substring

text.indexOf("acme") !== -1 also matches "acmeville". In an Intuitive AI refiner, that picks the wrong vendor subclass. Use word boundaries and escape the search text:

containsWord
function escapeRegExp(s) {
    return String(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function containsWord(text, word) {
    return new RegExp("\\b" + escapeRegExp(word) + "\\b", "i").test(String(text || ""));
}
// containsWord("Invoice from Acme Corp", "acme")     -> true
// containsWord("Invoice from Acmeville Ltd", "acme") -> false

Quick reference

Pattern text, ready for an Input Mask or a Regex Anchor. In code, put it between slashes.

Value Pattern
Email ^[^\s@]+@[^\s@]+\.[^\s@]+$
US ZIP ^\d{5}(-\d{4})?$
US phone ^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
US SSN ^\d{3}-?\d{2}-?\d{4}$
ISO date ^\d{4}-\d{2}-\d{2}
Digits only ^\d+$
Letters and digits ^[A-Za-z0-9]+$