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
  • 34,719
  • 24
  • 185
  • 251

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
  • 62,337
  • 13
  • 111
  • 105