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
| Name | Type | Description |
|---|---|---|
| string | string | Observed text. |
| regexp | RegExp | Expected pattern. |
| message | string | Error | Failure 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);}