I'm having trouble understanding what I did wrong in the regex below:
/^([^#]+)#(\1.*?(?=\||$))/gi
It correctly matches foo#foo,xyz,asd123|bar,asd123,xyz
, with the desired result being foo,xyz,asd123
.
But it does not match bar#foo,xyz,asd123|bar,asd123,xyz
. Expected output would be bar,asd123,xyz
.
Basically, I need to use the result of the capture group 1 to search further in the string after the # character. However, it's only working for the match immediately after # and nothing else. I feel like I'm missing a very basic thing here.
You can use
^([^#]+)#.*?(\1.*?)(?=\||$)
Details
^
- start of string([^#]+)
- Group 1 (\1
): one or more chars other than #
#
- a #
char.*?
- zero or more chars other than line break chars as few as possible(\1.*?)
- Group 2: same value as captured into Group 1 and then any zero or more chars other than line break chars as few as possible(?=\||$)
- a positive lookahead that requires |
or end of string immediately to the right of the current location.