I am a newbie to JS RegExp. I got confused by the following RegExp matches.
var x = "red apple"
var y = x.match(/red|green/i)
Now y is ["red"].
However if I add a pair of parentheses around red and green and make y to be
var y = x.match(/(red|green)/i)
Now, y would become ["red", "red"]. I have searched online https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
and found that it's something called
capturing parentheses.
It says For example, /(foo)/ matches and remembers 'foo' in "foo bar." The matched substring can be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.
But I don't understand what does it mean by recalled from the resulting array's element or from predefined RegExp object's properties? Can anyone please explain ? Thank you!