📌  相关文章
📜  带有变量的 js 正则表达式 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:38.808000             🧑  作者: Mango

代码示例3
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' ]