Javascript用斜杠替换双引号

时间:2022-03-28 16:48:49

I want to replace " with \" with Javascript.

我想用Javascript替换“\”。

I have:

我有:

text = text.toString().replace("\"", '\\"')

The result:

结果:

\\"

2 个解决方案

#1


27  

Try this:

试试这个:

text = text.toString().replace(/"/g, '\\"')

Or this:

或:

text = text.toString().replace('"', '\\"')

#2


1  

This will do:

这将会做的事:

text = text.toString().replace("\"", '\\\"');

You basically have to escape both '\' and '"' by \

你基本上必须同时避开‘\’和‘\’

#1


27  

Try this:

试试这个:

text = text.toString().replace(/"/g, '\\"')

Or this:

或:

text = text.toString().replace('"', '\\"')

#2


1  

This will do:

这将会做的事:

text = text.toString().replace("\"", '\\\"');

You basically have to escape both '\' and '"' by \

你基本上必须同时避开‘\’和‘\’