如何使用 javascript 字符串函数将 \ 替换为 \\

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3913664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:40:30  来源:igfitidea点击:

how to replace \ with \\ using javascript string functions

javascriptstring

提问by mahesh

I have the following filepath

我有以下文件路径

var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"

how can I replace \ with \ so so that it prints

我怎样才能用 \ 替换 \ 以便它打印

var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"

回答by Andy E

You need to use the replace()method whilst escaping the \character on each occurrence:

您需要使用replace()方法,同时\在每次出现时转义字符:

imagepath = imagepath.replace(/\/g, "\\");
//-> "C:\Documents and Settings\Mahesh\Desktop\images\advts.png"

If imagepathis defined with single backslash characters, those will be evaluated into an escape sequence along with the character(s) following them. At this point it's too late to replace the backslashes as they are removed from the resulting string.

如果imagepath使用单个反斜杠字符定义,则这些字符将与后面的字符一起计算为转义序列。此时替换反斜杠为时已晚,因为它们已从结果字符串中删除。

回答by reko_t

It's a string literal.. By the time it's assigned to imagepath, the escape sequences are already parsed and processed, and the backslashes no longer exist in the string. You just have to have them in the string in the first place.

它是一个字符串文字。当它被分配给 imagepath 时,转义序列已经被解析和处理,并且字符串中不再存在反斜杠。您只需要首先将它们放在字符串中。

To demonstrate what I mean:

为了证明我的意思:

>>> var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
>>> imagepath
"C:Documents and SettingsMaheshDesktopimagesadvts.png"

There's no way to fix the issue at that stage.

在那个阶段没有办法解决这个问题。

回答by Buhake Sindi

You can use the string replace()method. Replace on W3Schools.com.

您可以使用字符串replace()方法。在 W3Schools.com 上替换

Example:

例子:

var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
document.write(imagepath.replace("\", "\\"));


UpateAs reko_tsaid, once a path is assigned to imagepath, it's a processed literal. The \characters disappears. I've just tested my code with this example and there's no way to fix it unfortunately.

Upate正如reko_t所说,一旦将路径分配给imagepath,它就是一个经过处理的文字。该\字符消失。我刚刚用这个例子测试了我的代码,不幸的是没有办法修复它。

<html>
    <head>
        <script type="text/javascript">
            function init() {
                var imagepath="C:\Documents and Settings\Mahesh\Desktop\images\advts.png"
                alert(imagepath.replace(/\/g, "\\"));
            }
        </script>
    </head>
    <body onload="init()">

    </body>
</html>

The only way to do it, is to replace \to \\on server side, and assign it to imagepathbefore it gets rendered to javascript that needs to be used by javascript.

唯一的方法是在服务器端替换\\\,并imagepath在它被渲染到需要由 javascript 使用的 javascript 之前将其分配给。

回答by Wazy

Just add after var imagepath initialisation

只需在 var imagepath 初始化后添加

var imagepath=imagepath.replace("\", "\");

return imagepath;

返回图像路径;

回答by Sekhar T

Try this :

试试这个 :

str.replace(new RegExp('/', 'g'),'-')