获取字符串将其转换为正则表达式

使用regexp对象构造函数从字符串创建正则表达式:

const value = "176****3333";
const str = "(^$)|(^0?(13|14|15|17|18|19)[0-9]{9}$)";
const reg = new RegExp(str);
console.log(reg);

if (!reg.test(value)) {
  console.log("手机号格式错误");
}

更多推荐

JS将字符串转换为正则表达式