This is not a bug. This is one of the quirks of regex engines. I've mentioned it before in another bug report.
$regexp(test,(.*),$1 oops)
.*matches the whole string (start of the match at offset 0).- Match ("test") is captured into first backreference.
- Match is replaced with
$1 oopsso that "test" becomes "test oops". .*matches void after the string (start of match at offset 1, zero-width match). Star makes the dot optional so that the pattern can match here!- Match ("") is captured into first backreference. That's right! The first backreference exists and simply holds nothingness.
- Match is replaced with '$1 oops' so that "" becomes " oops"
After all, our input string "test" becomes "test oops oops".