Clone RegExp

OSCC · JavaScript, Type · Oct 22, 2020

Clones a regular expression.

  • Use the RegExp constructor, RegExp.prototype.source and RegExp.prototype.flags to clone the given regular expression.
const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
const regExp = /lorem ipsum/gi;
const regExp2 = cloneRegExp(regExp); // regExp !== regExp2

Recommended snippets

  • Array is empty

    JavaScript, Type

    Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.

  • Value is promise-like

    JavaScript, Type

    Checks if an object looks like a Promise.

  • Value is async function

    JavaScript, Type

    Checks if the given argument is an async function.