双反斜杠在节点中打印相同 [英] Double backslash prints the same in node

查看:38
本文介绍了双反斜杠在节点中打印相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个目录字符串变量,它是 '.\app\src\algorithms' 以在 Windows 平台节点的 exec 函数中使用它.但是,即使在字符串中使用双反斜杠,它也无法正常工作.这是我的尝试;

I want to create a directory string variable which is '.\app\src\algorithms' to use it in exec function in node on Windows Platform. However, it doesn not work properly even use double backslashes in the string. Here is my try;

λ node
> directory =  '.\app\src\algorithms';
'.appsrcalgorithms'
> directory =  '.\\app\\src\\algorithms';
'.\\app\\src\\algorithms'

推荐答案

你所拥有的很好.在内部,它存储为双反斜杠,因为这就是在 JS 字符串中转义反斜杠的方式.节点 REPL 向您显示实际值.当您使用它时,它应该正确呈现.

What you have is fine. Internally it's stored as a double backlash because that's how escaping backslashes works in JS strings. The node REPL is showing you the actual value. When you use it, it should render correctly.

> directory = '.\\app\\src\\algorithms';
'.\\app\\src\\algorithms'
> console.log(directory)
.\app\src\algorithms
> exec('explorer.exe ' + directory); //works

这篇关于双反斜杠在节点中打印相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆