Javascript use variable inside match regex [duplicate]

Use the RegExp constructor, not the literal. That allows you to do string concatenation or interpolation as you please:

let n = 20;
let r = new RegExp(".{1," + n + "}", "g");

text.match(r);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top