Replace double backslashes with a single backslash in javascript

后端 未结 2 1016
慢半拍i
慢半拍i 2021-01-04 14:27

I have the following problem:

I have a script that executes an AJAX request to a server, the server returns C:\\backup\\ in the preview. However, the r

相关标签:
2条回答
  • 2021-01-04 14:44

    Best is to use regex to replace all occurrences:

    C:\\backup\\".replace(/\/\//g, "/")
    

    this returns: C:\backup\

    OR

    use split()

    "C:\\backup\\".split();
    

    both produces your desired result

    C:\backup\

    console.log("using \"C:\\backup\\\".replace(/\/\//g, \"/\")")
    console.log("C:\\backup\\".replace(/\/\//g, "/"));
    
    console.log("Using \"C:\\backup\\\".split()");
    console.log("C:\\backup\\".split());

    0 讨论(0)
  • 2021-01-04 14:51

    This should do it: "C:\\backup\\".replace(/\\\\/g, '\\')

    In the regular expression, a single \ must be escaped to \\, and in the replacement \ also.

    0 讨论(0)
提交回复
热议问题