Tambahkan variabel di dalam regex di javascript
var pattern="cd"
var repeats=3
new RegExp(`${pattern}{${repeats}}`, "g")
Calm Cardinal
var pattern="cd"
var repeats=3
new RegExp(`${pattern}{${repeats}}`, "g")
function alphabet(str) {
let alphabetStr = 'abcdefghijklmnopqrstuvwxyz';
let strRegEx =`[${str[0]}-${str[3]}]`; //set regex with variables
let newRegEx = new RegExp(strRegEx, 'g'); //create new regex object
//regex object pattern is: RegExp(pattern, modifiers)
let charSetStr = alphabetStr.match(newRegEx);
return charSetStr;
}
console.log(alphabet('abce'));
//expected [ 'a', 'b', 'c', 'd', 'e' ]
// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")