Answers for "js regex with variable"

6

javascript use variable regex

// CREATE A REGEXP object
var replace = "regex";
var re = new RegExp(replace,"g");
//then use
"mystring".replace(re, "newstring")
Posted by: Guest on September-11-2020
2

add variable inside regex in javascript

var pattern="cd"
var repeats=3
new RegExp(`${pattern}{${repeats}}`, "g")
Posted by: Guest on December-16-2020
1

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' ]
Posted by: Guest on August-20-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language