Eight backslashes required to replace single backslash with double backslashes?

后端 未结 6 513
借酒劲吻你
借酒劲吻你 2021-01-18 09:37

This is a \"what the heck is going on here\" question. I don\'t actually need a solution.

I had to replace all single backslashes in a String with double backslashe

6条回答
  •  感动是毒
    2021-01-18 10:14

    Yep, it gets hairy when you need to do this sort of thing, doesn't it.

    The reason you need so many backslashes is that you need to take into account that backslash is used for both escaping a string and for escaping a regex.

    • Take 1 backslash.
    • Double it for string escaping.
    • Double it again for regex escaping.
    • Double it again because you need to match two consecutive backslashes in your original string.

    That makes 8.

提交回复
热议问题