chuck icon indicating copy to clipboard operation
chuck copied to clipboard

Weird regex replacement behaviour

Open wasamasa opened this issue 9 years ago • 0 comments

"\"\"" => string x;
<<< x >>>;
<<< RegEx.replaceAll("\"", "\\\"", x) >>>;
// expected it to print "\"\"", got "\""" instead

"\"f\"" => string y;
<<< y >>>;
<<< RegEx.replaceAll("\"", "\\\"", y) >>>;
// this on the other hand prints "\"f\"" as expected

I ended up writing my own replacement function that operates on plain strings.

Compare with Ruby:

>> puts "\"\"".gsub(/\"/, "\\\"")
\"\"
=> nil

wasamasa avatar May 12 '16 07:05 wasamasa