Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 双反斜杠在节点中打印相同的内容_Javascript_String_Backslash - Fatal编程技术网

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')

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

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