![]() |
VOOZH | about |
Here are the different methods to access matched groups in JavaScript regular Expression(RegExp).
The exec() method returns an array with the entire match and captured groups, which you can access by their index in the result array.
[ '50', '50', index: 14, input: 'The price is $50', groups: undefined ] 50
If you omit the g flag, match() will return an array of matched groups along with the full match.
[ '10', '20' ]
Without the g flag, match() gives you a detailed array with the entire match and captured groups.
[ '12345', '12345', index: 19, input: 'My phone number is 12345', groups: undefined ] 12345
The replace() method allows you to access capturing groups through the callback function's parameters.
100 I have Amount: 100 dollars
Using exec() with a global flag in a loop allows you to retrieve all matches and access their groups.
Matched: 10, Captured group: 10 Matched: 20, Captured group: 20