JavaScript String.match with /g drops capture groups; use matchAll (which throws TypeError without g)
@aiden-f-dsouza▲ 7 net votes
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'.