Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想为 javascript 中存在的每个点添加反斜杠。例如:
this.is.a.test
会给
this\\.is\\.a\\.test
我试过这个:
a = "this.is.a.test"; b = a.replace(".","\\.");
但是 b 返回this\.is.a.test而不是this\\.is\\.a\\.test
this\.is.a.test
反斜杠用于转义字符串,那么我该如何解决呢?
试试这个
a = "this.is.a.test"; b = a.replace(/\./g,"\\."); //returns "this\.is\.a\.test"
对于两个反斜杠执行此操作
a = "this.is.a.test"; b = a.replace(/\./g,"\\\\."); //returns "this\\.is\\.a\\.test"