let str2 = 'mom and dad and baby';
let rex3 = /mom( and dad (and baby)?)?/g;
console.log(rex3.exec(str2));
console.log(rex3.exec(str2)[0])
console.log(rex3.exec(str2)[1]);
console.log(rex3.exec(str2)[2]);
let str2 = 'mom and dad and baby';
let rex3 = /mom( and dad (and baby)?)?///g;//去掉全局匹配,要么会从上一的匹配位置继续往后查找
console.log(rex3.exec(str2));
console.log(rex3.exec(str2)[0])
console.log(rex3.exec(str2)[1]);
console.log(rex3.exec(str2)[2]);
返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
你第一个打印是否匹配上了?未匹配的话是无法获取下标为0的数组