Assertions

assert.match()

Asserts that a string matches a regular expression. Use it for bounded text and fixed patterns, not for proving that complex structured output is correct. Tested on Node.js 22.16.0. Example output (one line per entry): ["ERR_ASSERTION"].

Formal Syntax & Structure

nodejs syntaxnodejs
assert.match(string, regexp[, message])

Parameters & Attributes

NameTypeDescription
stringstringObserved text.
regexpRegExpExpected pattern.
messagestring | ErrorFailure detail.

Return Value

undefined

Returns normally or throws.

Working Example

Example Snippetnodejs
import assert from 'node:assert/strict';assert.match('book-42', /^book-\d+$/);try {    assert.match('wrong', /^book-\d+$/);}catch (e) {    console.log(e.code);}
Node.js Reference