Given this string:
z;
hh ;d;
;
;
;
12;b
;
bb;b
;;;
;;
And this expression:
^(?:;+)\R*
I get 2 different results in regex101.com and regexr.com. The difference being that regex101.com has an extra \n at the end, in the substitution box and regexr.com does not
Questions:
1) From my limited regex experience regexr.com is showing the correct answer, is this correct?
2) Are differences like these bugs or simply differences in implementations? So basically they are both correct?
Both regex testers produce the same result. The missing new line in regexr.com is only an illusion, caused by the HTML element containing the replacement result having an attribute of readonly
, making you unable to put your cursor on the new line character. But just because you can't put your cursor on it, doesn't mean it doesn't exist.
If you clear both the regex and replacement fields, so that whatever is in the "text" field goes straight to the "result". Then write in the "text" field a single line, press enter to insert a new line. You should be unable to find that new line character in the "result" area.
Therefore, this is just a quirk of regexr.com, not an issue specific to this particular regex.
If you want to be able to put your cursor on it, go to your browser's dev tools, inspect the "result" area, and you'll see it is a textarea
element like this:
<div class="result">
<textarea readonly>
...
</textarea>
</div>
Delete the word readonly
, and you should be able to put your cursor on the new line character.
Both regex testers are correct. Although you removed the new line characters after each match using \R
, the last new line character in the result actually comes from the new line character ending the line bb;b
. Since bb;b
is not a match, this new line is not removed.