Double backslash prints the same in node

前端 未结 2 1524
挽巷
挽巷 2021-01-29 04:01

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 ev

相关标签:
2条回答
  • 2021-01-29 04:25

    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')
    
    0 讨论(0)
  • 2021-01-29 04:38

    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
    
    0 讨论(0)
提交回复
热议问题