Dev Helpers

Node.js / Serialization

Preserve a regular expression

Dev Helpers · January 27, 2026

Cloning a regular expression should preserve the source and flags your caller relies on. Stateful matching can require additional attention to position.

Worked example

import { serialize, deserialize, Serializer, Deserializer } from "node:v8";
const roundTrip = (value) => deserialize(serialize(value));
console.log(roundTrip(/hello/gi).flags);

Result

gi

Implementation notes

Flags alter matching behaviour even with the same source text. A copied expression without the original flags can pass simple tests and fail on real input.

Try it yourself

Compare flags and lastIndex before and after the copy.

Open the Node.js example →

Editorial date January 27, 2026 · 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.