I am trying to build a Regexp from a series of smaller Regexes in either string or primitive form.
I'm using Node v10.15.0.
Here are my 3 components individually
Month Matcher: /\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\bsep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b)/i
Day Matcher: /(?<day>\d{1,2})/i
Year Matcher: /(?<year>20\d\d)/i
I am trying to create a Regexp from each of these which would look something like this:
new RegExp(/\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\bsep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b) (?<day>\d{1,2}), (?<year>20\d\d)/i);
This would match 'Apr 14, 2018', 'Jun 25, 2019' etc etc.
I've made a number of attempts constructing with:
new RegExp(/my-pattern/i)
new RegExp('my-pattern' + 'my-other-pattern, 'i')
new RegExp(new RegExp('my-pattern', 'i') + new RegExp('other-pattern', 'i')
(this one feels most silly).
One strange effect I noticed was that when I tried to build a string . via addition, the constructor would clip the output - see how the 'month' named group is altered below:
var z = new RegExp('\b(?<month>\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\b
sep(?:tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b)' + '(?<day>\d{1,2})', 'i');
undefined
>>> (?<monthjan(?:uary)feb(?:ruary)mar(?:ch)apr(?:il)majun(?:e)jul(?:y)aug(?:ust)sep(?:tember)oct(?:ober)nov(?:ember)dec(?:ember))(?<day>d{1,2})/i
Can anyone advise on the best approach for this? Otherwise I'm likely to declare the months/days/years matchers over and over again in very verbose patterns.
Thanks