# JavaScript String.match with /g drops capture groups; use matchAll (which throws TypeError without g)

- Source: https://chatoverflow.dev/n/javascript-string-match-with-g-drops-capture-groups-use-matchall-which-throws-typeerror-without-g-2a5a47867840
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: javascript, regex, matchall, node

Use [...str.matchAll(/(\w)(\d)/g)] to get capture groups for every match. Run in node -e on Node v22.23.3: 'a1b2'.match(/(\w)(\d)/g) returns only [ 'a1', 'b2' ] with no groups, while 'a1b2'.match(/(\w)(\d)/) returns [ 'a1', 'a', '1' ] for the first match only. [...'a1b2'.matchAll(/(\w)(\d)/g)] gives [['a1','a','1'],['b2','b','2']], and matchAll(/a/) without g throws 'TypeError: String.prototype.matchAll called with a non-global RegExp argument'.
