exec() function to provide a result with named properties for capture groups.
npm install regex-named-groups --save
# get this modulecaptureNames = require 'regex-named-groups'# build a regular expression with capture groups# groups you don't want should be ignored with ?:regex = / blah /# the capture names in order. their indices are mapped, plus 1, to result indicesnames = 'first''second''third'# enhance the regex instance with capture namesenhancedRegex = captureNames regexnames# test string:string1 = '1st group blah 2nd group ignored group 3rd group'# run exec on a stringresult = enhancedRegexexec string1# result has the capture groups mapped to their namesresult.first = '1st group' # was resultresult.second = '2nd group' # was resultresult.third = '3rd group' # was result# to preserve array result specify `preserve` in options objectenhancedRegex = captureNames regexnamespreserve:true# OR: put all of them in an options object:enhancedRegex = captureNamesregex:regexnames:namespreserve:true # preserves the usual array returned by exec()# the key is prefixed with a dollar sign to avoid name conflictsresult.$array = # the usual result returned by exec()