A regex argument of | needs to be escaped twice, once to escape the slash for Java and again to escape the | for regex. This is because | is a regex operator, so, to use it as a ‘to look for’ character, it must be escaped.
Conversely, when used in the ‘replacement string’ position, it doesn’t need to be escaped at all because | is not a special character to Java when it appears as a character or string. (| is used in conditional statements, but that’s another topic). The replacement string argument is simply placed in the target string where it matches the regex.