Files
30-seconds-of-code/snippets/cloneRegExp.md
Isabelle Viktoria Maciohsek c3a2e47672 Add prototype to descriptions
2020-10-20 11:21:07 +03:00

384 B

title, tags
title tags
cloneRegExp regexp,intermediate

Clones a regular expression.

  • Use new RegExp(), 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