how to escape characters in nodejs?

前端 未结 2 451
感动是毒
感动是毒 2021-01-11 10:46

I was wondering how would you escape special characters in nodejs. I have a string $what$ever$ and I need it escaped like \\$what\\$ever\\$ before i call a python script wit

2条回答
  •  一生所求
    2021-01-11 11:07

    You can do this without any modules:

    str.replace(/\\/g, "\\\\")
       .replace(/\$/g, "\\$")
       .replace(/'/g, "\\'")
       .replace(/"/g, "\\\"");
    

提交回复
热议问题