本文主要是介绍面试题正则,?和小括号()的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
var con= “mom and dad and baby”;
var regP= /mom (and dad (and baby)?)?/ig;
var matches = regP.exec(con);
// 问题 console.log(matches[2]);输出什么?
console.log(matches[2]);
执行结果:
console.log(matches[2]);//and baby
matches
(3) [“mom and dad and baby”, “and dad and baby”, “and baby”, index: 0, input: “mom and dad and baby”, groups: undefined]0: "mom and dad and baby"1: "and dad and baby"2: "and baby"groups: undefinedindex: 0input: "mom and dad and baby"length: 3__proto__: Array(0)
菜鸟上类似的例子:https://www.runoob.com/regexp/regexp-syntax.html
这篇关于面试题正则,?和小括号()的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!