node.js - __dirname doesn't work in a regex -
i trying remove __dirname directory need copy (recursively). gather information problem:
console.log(typeof __dirname); // string console.log(__dirname); // c:\test
i need create regex string program. therefore must use regexp(mystring)
. reality check on jsfiddle make sure right way escape \ \/ fiddle.
i run code in browser , works. run code in node.js , not work. take extreme trying remove regexp(__dirname)
__dirname.
if have string var s = __dirname.tostring() + "myotherpath/a.cat"
how remove __dirname part of string s?
your regex example wrong. regex removing backslash. string not have either backslash or slash.
var y = "c:\y"; //"c:y"
to correctly add backslash have
var y = "c:\\y"; //"c:\y"
your example have worked in linux separator /, not need escaped. besides looks doing substring replace not regex replace. giving __dirname in replace suffice :
var y = __dirname; var z = y.replace(__dirname,"").tostring();
Comments
Post a Comment