Named capture failed
bluelovers opened this issue · 2 comments
bluelovers commented
node.js v14.4.0
import XRegExp from 'xregexp';
var repeatedWords = XRegExp('\\b(?<word>[a-z]+)\\s+\\k<word>\\b', 'gi');
console.dir(repeatedWords.exec('The the test data'))
console.dir('The the test data'.match(repeatedWords))[
'The the',
'The',
index: 0,
input: 'The the test data',
groups: undefined
]
[ 'The the' ]
slevithan commented
You need to use XRegExp.exec. This will work:
XRegExp.exec('The the test data', repeatedWords).word; // -> 'The'Or with namespacing:
XRegExp.install('namespacing');
XRegExp.exec('The the test data', repeatedWords).groups.word; // -> 'The'Ideally XRegExp should be updated in a new major version to have the namespacing feature turned on by default. It should probably also be updated to defer to native handling of named capturing groups in ES2018 browsers/etc. (where native handling was introduced). That way, you could process regexes created by XRegExp with RegExp.prototype.exec (like in your example code) rather than XRegExp.exec and still get the intended result.