<!DOCTYPEhtml><htmllang="zh"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>测试2</title></head><body><script>let text ="mom and dad and baby";let pattern =/mom( and dad( and baby)?)?/gi;let matches = pattern.exec(text);
console.log(matches.index);// 0
console.log(matches.input);// "mom and dad and baby"
console.log(matches[0]);// "mom and dad and baby"
console.log(matches[1]);// " and dad and baby"
console.log(matches[2]);// " and baby"
console.log('!!!!!!!!!!!!')const result = text.match(pattern)
console.log(result)</script></body></html>
无g时
<!DOCTYPEhtml><htmllang="zh"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>测试2</title></head><body><script>let text ="mom and dad and baby";let pattern =/mom( and dad( and baby)?)?/;let matches = pattern.exec(text);
console.log(matches.index);// 0
console.log(matches.input);// "mom and dad and baby"
console.log(matches[0]);// "mom and dad and baby"
console.log(matches[1]);// " and dad and baby"
console.log(matches[2]);// " and baby"
console.log('!!!!!!!!!!!!')const result = text.match(pattern)
console.log(result)</script></body></html>