简体   繁体   English

在 Javascript 内写括号替换

[英]Write Brackets Inside Javascript Replace

function myFunction() {
  var text = document.getElementById("ansiText").value; 
  var finalText = text.replace(/)/g, "right");
 
  document.getElementById("ansiText").value = finalText;
}

I want to find and replace ( as left and ) as right .我想找到并替换( left) right But its didn't work if i write ) in code, its create errors.但是如果我在代码中编写)它不起作用,它会产生错误。 How can I write ( in replace function?我怎么写(代替function?

Just escape the brackets to fix this: text.replace(/\)/g, "right")只需转义括号即可解决此问题: text.replace(/\)/g, "right")

function myFunction() {
  var text = document.getElementById("ansiText").value; 
  var finalText = text.replace(/\)/g, "right");
 
  document.getElementById("ansiText").value = finalText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM