Double backslash prints the same in node

风流意气都作罢 提交于 2020-01-30 10:56:07

问题


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'

回答1:


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



回答2:


I think the best way to handle platform independent work with path is using path module. E.g.

var path = require('path');
var directory = path.join('.', 'app', 'src', 'algorithms')


来源:https://stackoverflow.com/questions/37089415/double-backslash-prints-the-same-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!