Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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' ]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #js #regex #variable
ADD COMMENT
Topic
Name
5+7 =