How to clone a given regular expression in JavaScript?

JavaScript's regular expression support is widely considered to be among the best in the world, but there's one area where it's lacking: there's no built-in way to clone a regular expression. This can be a problem when you need to create a new regular expression that is similar to an existing one but with a few small changes.

The problem is that regular expressions are objects, and when you assign one regex to another variable, both variables reference the same object in memory.

The Problem with Direct Assignment

let regex1 = /foo/g;
let regex2 = regex1;

console.log("Same object?", regex2 === regex1);
console.log("regex1 lastIndex:", regex1.lastIndex);

// Using regex1 changes its internal state
"foo bar foo".match(regex1);
console.log("After using regex1, lastIndex:", regex1.lastIndex);
console.log("regex2 affected too:", regex2.lastIndex);
Same object? true
regex1 lastIndex: 0
After using regex1, lastIndex: 3
regex2 affected too: 3

Method 1: Using RegExp Constructor

The most reliable way to clone a regular expression is using the RegExp constructor with the source pattern and flags:

let original = /hello/gi;
let cloned = new RegExp(original.source, original.flags);

console.log("Original:", original);
console.log("Cloned:", cloned);
console.log("Same object?", original === cloned);
console.log("Same pattern?", original.toString() === cloned.toString());
Original: /hello/gi
Cloned: /hello/gi
Same object? false
Same pattern? true

Method 2: Creating a Reusable Clone Function

function cloneRegex(regex) {
    return new RegExp(regex.source, regex.flags);
}

let original = /test\d+/gim;
let clone1 = cloneRegex(original);
let clone2 = cloneRegex(original);

console.log("Original:", original);
console.log("Clone 1:", clone1);
console.log("Clone 2:", clone2);
console.log("All different objects:", original !== clone1 && clone1 !== clone2);
Original: /test\d+/gim
Clone 1: /test\d+/gim
Clone 2: /test\d+/gim
All different objects: true

Method 3: Cloning with Modified Flags

You can also clone a regex while modifying its flags:

let original = /hello/g;
let caseInsensitive = new RegExp(original.source, original.flags + 'i');
let multiline = new RegExp(original.source, 'gm');

console.log("Original flags:", original.flags);
console.log("With 'i' flag:", caseInsensitive.flags);
console.log("With 'gm' flags:", multiline.flags);
Original flags: g
With 'i' flag: gi
With 'gm' flags: gm

Why Clone Regular Expressions?

Cloning regular expressions is useful when:

  • State preservation: Global regexes maintain a lastIndex property that changes during matching
  • Multiple instances: You need multiple independent copies for concurrent operations
  • Flag variations: Creating similar patterns with different flags

Comparison of Methods

Method Complexity Preserves Flags Reusable
Direct RegExp constructor Low Yes No
Custom clone function Medium Yes Yes
With flag modification Medium Customizable Yes

Practical Example

let emailPattern = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
let text = "Contact: john@example.com or jane@test.org";

// Clone to avoid state interference
let clone1 = new RegExp(emailPattern.source, emailPattern.flags);
let clone2 = new RegExp(emailPattern.source, emailPattern.flags);

console.log("First search:", text.match(clone1));
console.log("Second search:", text.match(clone2));
console.log("Both found emails independently");
First search: [ 'john@example.com', 'jane@test.org' ]
Second search: [ 'john@example.com', 'jane@test.org' ]
Both found emails independently

Conclusion

Use new RegExp(regex.source, regex.flags) to create true copies of regular expressions. This prevents state interference and allows independent usage of regex patterns in your applications.

Updated on: 2026-03-15T23:19:00+05:30

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements