Dev Helpers

Node.js / Runtime types

Recognize a regular expression

Dev Helpers · September 18, 2025

A regular expression may arrive from a different execution context. A runtime type check can be more reliable than inspecting a constructor name.

Worked example

import { types } from "node:util";
console.log(types.isRegExp(/node/i));

Result

true

Implementation notes

An object with a source property is not necessarily a regular expression. Accept only the runtime types the caller actually supports.

Try it yourself

Compare a real expression with an object that merely has a test method.

Open the Node.js example →

Editorial date September 18, 2025 · Published December 6, 2026

Related articles

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.