regex-named-groups
Enhances the exec()
function to provide a result with named properties for capture groups.
Install
npm install regex-named-groups --save
Usage
# get this module captureNames = 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 indices names = 'first''second''third' # enhance the regex instance with capture names enhancedRegex = captureNames regexnames # test string: string1 = '1st group blah 2nd group ignored group 3rd group' # run exec on a string result = enhancedRegexexec string1 # result has the capture groups mapped to their names result.first = '1st group' # was result[1] result.second = '2nd group' # was result[2] result.third = '3rd group' # was result[3] # to preserve array result specify `preserve` in options object enhancedRegex = captureNames regexnamespreserve:true# OR: put all of them in an options object: enhancedRegex = captureNames regex:regex names:names preserve:true # preserves the usual array returned by exec() # the key is prefixed with a dollar sign to avoid name conflicts result.$array = # the usual result returned by exec()