Javascript 双反斜杠在节点中打印相同的内容

Javascript 双反斜杠在节点中打印相同的内容,javascript,string,backslash,Javascript,String,Backslash,我想创建一个名为“.\app\src\algorithms”的目录字符串变量,以便在Windows平台上节点的exec函数中使用它。 但是,即使在字符串中使用双反斜杠,它也不能正常工作。 这是我的尝试 λ node > directory = '.\app\src\algorithms'; '.appsrcalgorithms' > directory = '.\\app\\src\\algorithms'; '.\\app\\src\\algorithms' 我认为使用pat

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

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

我认为使用path处理平台无关工作的最佳方法是使用path模块。例如

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

我认为使用path处理平台无关工作的最佳方法是使用path模块。例如

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

你所拥有的一切都很好。在内部,它存储为双重反作用,因为这就是JS字符串中转义反斜杠的工作方式。节点REPL将向您显示实际值。使用它时,它应该正确渲染

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