0

Let's say I have the ff regex:

var str = 'hi there I am';

function testing(input, int){

  var par = input.match(`/.{1, ${int}}/g`);
  console.log(par);
}

console.log(testing(str, 5));

How can I passed in the argument/variable nto make it work on the regex?

Is there a way to do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

You can use template literals

function maxTotal(n){
  console.log(`/.{1,${n}}/g`);
  //var max = total.match(`/.{1,${n}}/g`);
}

maxTotal(4);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112