javascript use variable regex
// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")
javascript use variable regex
// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")
add variable inside regex in javascript
var pattern="cd"
var repeats=3
new RegExp(`${pattern}{${repeats}}`, "g")
js regex with variable
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' ]
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us