I was trying this match
'/links/51f5382e7b7993e335000015'.match(/^\/links\/([0-9a-f]{24})$/g)
and got:
['/links/51f5382e7b7993e335000015']
while I was expecting:
['/links/51f5382e7b7993e335000015', '51f5382e7b7993e335000015']
I had no luck until I removed the global flag, which I did not think would impact results my results!
With the global flag removed,
'/links/51f5382e7b7993e335000015'.match(/^\/links\/([0-9a-f]{24})$/)
produced:
[ '/links/51f5382e7b7993e335000015',
'51f5382e7b7993e335000015',
index: 0,
input: '/links/51f5382e7b7993e335000015' ]
which is cool, but reading the docs I can't figure out:
- Why the first form didn't work
- Why the global flag interfered with the
()
matching - How to get my expected result without the
index
andinput
properties
On JavaScript Regex and Submatches the top answer says:
Using String's match() function won't return captured groups if the global modifier is set, as you found out.
However,
> 'fofoofooofoooo'.match(/f(o+)/g)
["fo", "foo", "fooo", "foooo"]
seems to produce captured groups just fine.
Thank you.