Reputation: 3222
This is going to be a really short question...
Lets assume I have a regex like:
(Hello)(?:fooBar)
How can I backreference it using \2? I know this works with normal capturing groups and that a non capturing group only groups tokens together but if there is any work-around then I would be glad to know.
Edit: Problem is:
I need to backreference to non capturing groups sometimes. For example there are moments where I really dont want that group to show up as a match but it shows up multiple times in the pattern which is why I prefer to backreference it.
Upvotes: 1
Views: 548
Reputation: 88378
As mentioned in the comments, a backreference applies only to a capture group.
But it sounds like your actual concern is with not repeating yourself. In this case, you can construct the regex:
const f = 'foobar';
const r = new RegExp(`Hello${f}blah${f}blah${f}blah`);
Upvotes: 2