Skip to content Skip to sidebar Skip to footer

Why Does String.match Return Duplicate Item In Result Array?

Why does 'abc123'.match(/(\d{3})/) return [ '123', '123' ] instead of just ['123'] Isn't the expression equivalent to find exactly three digits?

Solution 1:

It returns two results because you've used a capturing group.

In the results array, results[0] will contain what was matched by the complete expression, results[1] will contain what was matched by the first capturing group, and so on.

In your case, both the complete expression and the first group yield the same result.

Post a Comment for "Why Does String.match Return Duplicate Item In Result Array?"