在js里面使用正则表达式

发布于 6 年前作者 weichang5042 次浏览最后编辑 6 年前来自 ask

let regexp = new RegExp(‘\d’);

const lastCheck = regexp.exec(‘2019-05-12’); //2019-05-12

console.log(“lastCheck”, lastCheck)

控制台输出

lastCheck null

请问是怎么回事?

1 回复
xiaguiying
xiaguiying1 楼4 年前

用构造函数要转义\,因为传入的是字符串

let regexp = new RegExp(‘\\d’)

或用字面量定义则不用转义\(但需要转义/)

let regexp = /\d/