You need to avoid a double slash once for the string engine and once for the regular expression engine:
replaceAll("'","\\\\'")
Caution:. While this answers the question of how to insert a backslash in a string, of course, it should not be used to try to suppress SQL injection attacks.
To clarify: Imagine someone sending a line where the apostrophe has already escaped. This regular expression will cause the apostrophe to not be hidden (because now the backslash will disappear). So you need this regular expression to escape the apostrophe only if it is preceded by an even number of backslashes. It means
replaceAll("(?<!\\\\)((?:\\\\\\\\)*)'", "$1\\\\'")
It quickly becomes as inconspicuous as it seems, and it still does not apply to all cases.
source share