Extracts capturing group values via a regular expression
$ npm install @absolunet/capturing-group-values
import capturingGroupValues from '@absolunet/capturing-group-values';
const pattern = /(?<alpha>[a-z]+)?:(?<num>\d+)?/u;
console.log(capturingGroupValues('', pattern)); // {}
console.log(capturingGroupValues(':', pattern)); // {}
console.log(capturingGroupValues('abc', pattern)); // {}
console.log(capturingGroupValues('123', pattern)); // {}
console.log(capturingGroupValues('abc:xyz', pattern)); // { alpha: 'abc' }
console.log(capturingGroupValues('123:789', pattern)); // { num: '789' }
console.log(capturingGroupValues('abc:789', pattern)); // { alpha: 'abc', num: '789' }
View documentation
MIT © Absolunet