javascript 字符串替换 ( 为 \(

javascript string replace ( with \(

如何将字符串 "Test(5)" 动态转换为 "Test\(5\)"? (JQuery 或 Javascript)

我已经试过了,但没有成功

var string = "Test(5)";

string = string.replace("(","\(");
string = string.replace(")","\)");

console.log(string);

http://jsfiddle.net/mvehkkfe/

我假设你的意思是

replace the string "Test(5)" into "Test\(5\)"

在这种情况下:

var string = "Test(5)";

string = string.replace("(","\(");
string = string.replace(")","\)");

console.log(string);

转义反斜杠

如果你想把字符串"Test(5)"替换成"Test\(5\)",你可以使用下面的代码

var string = "Test(5)";

string = string.replace("(","\(");
string = string.replace(")","\)");

console.log(string);