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
giImplementation 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