简体   繁体   English

如何在Javascript中拆分转义字符\\

[英]How to split escape character \ in Javascript

How to split escape character in this string?如何在此字符串中拆分转义字符?

在此处输入图片说明

already tried doing this已经尝试过这样做

str = str.split('\\');

console.log(str);

this is the output这是输出

在此处输入图片说明

I want an output something like this我想要这样的输出

在此处输入图片说明

please help.请帮忙。 thank you.谢谢你。

The escape character is consumed when the string literal is parsed so there are no \\ characters in the string to replace.解析字符串文字时会使用转义字符,因此字符串中没有要替换的\\字符。

It is too late to fix this programmatically.以编程方式修复此问题为时已晚。 You need to edit the original source code to represent \\ characters as \\\\ .您需要编辑原始源代码以将\\字符表示为\\\\

You can't create a string like this, you have to use the double backslash.你不能创建这样的字符串,你必须使用双反斜杠。

var str = "..\..\common\core\services\shared.service";
console.log(str); // "....commoncoreservicesshared.service"

// Correct way
var str = "..\\..\\common\\core\\services\\shared.service";
console.log(str); // "..\..\common\core\services\shared.service"
// Then use this:
str.replace(/\\/g, '/');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM