双反斜杠在节点中打印相同

Double backslash prints the same in node

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

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

我认为处理与平台无关的路径工作的最佳方式是使用路径模块。例如

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

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

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