0

I've been trying to use regex to replace __dirname in logging. However it never seems to work. For instance, in an error stack: error.stack.includes(__dirname) returns true
error.stack.reaplce(/__dirname/g, ".\\") returns the stack trace unchanged

Manually writing out the directory name works, using .replace(__dirname, ".\\") also works, but obviously doesn't replace everything. I feel like this might just be me being stupid but, what am I doing wrong here?

Kotas
  • 229
  • 2
  • 6
  • `/__dirname/g` is literally replacing the string "__dirname" - one solution is to use `replaceAll` in your working attempt, or to construct a regex from the value if __dirname - though, you do have to escape any "special" (regex) characters – Bravo Jul 21 '21 at 00:22
  • You might need to create the regex differently `new RegExp(__dirname)`? – evolutionxbox Jul 21 '21 at 00:25
  • You have a typo in `reaplce`. Also, what is `__dirname` value? Please provide a JavaScript snippet to showcase the problem, right now, it is not clear what is going on. – Wiktor Stribiżew Jul 21 '21 at 09:46

0 Answers0