I am wondering how I can programmatically add \ in front of the spaces that appear in the file path. I use fs.readdir to get the contents of directories, and if I don't get out of the spaces from the path, I get an ENOENT error message because unescaped paths are not allowed on UNIX. I would like to get the result:
/path/with\ spaces/in/them/
However, I ran into a problem with REGEX in the NODE REPL. When I do this:
var path = '/path/with spaces/in/them/'; var escapedPath = path.replace(/(\s)/, "\\ ");
I get the result:
'/path/with\\ spaces/in/them/'
When I try to run the same code inside the Chrome console, I get:
"/path/with\ spaces/in/them/"
What is the desired effect.
I'm not sure if I missed something or if this is a mistake. I am confused because NODE runs on the same Javascript engine as Chrome, so I think these expressions will be interpreted the same way.
Please let me know if anyone knows a way around this. Maybe I'm missing something. I just need to get away from the paths before passing them to fs.readdir so that I can avoid these errors.
Thanks!
kevin source share