我可以使用此正则表达式在ckeditor中更改一个字,

editor = CKEDITOR.instances.txtisi;
var edata = editor.getData();
var rep_text = edata.replace("INSERT INTO", "INSERT-INTO");
editor.setData(rep_text);


但是如何添加更多可以替换的单词,而不仅仅是一个单词。我尝试过,但总能说出最后一句话。例如this.editor = CKEDITOR.instances.txtisi;

var edata = editor.getData();
var rep_text = edata.replace("INSERT INTO", "INSERT-INTO"); // you could also
var rep_text = edata.replace("DELETE TABLE", "DELETE-TABLE"); // you could also
var rep_text = edata.replace("TRUNCATE TABLE", "TRUNCATE-TABLE"); // you could also use a regex in the replace
editor.setData(rep_text);

最佳答案

您的代码中有一个错误

这是固定版本

      var edata = editor.getData();
      var edata = edata.replace("INSERT INTO", "INSERT-INTO"); // you could also
      var edata = edata.replace("DELETE TABLE", "DELETE-TABLE"); // you could also
      var edata = edata.replace("TRUNCATE TABLE", "TRUNCATE-TABLE"); // you could also use a regex in the replace
      editor.setData(edata);


原因是string.replace()返回一个新字符串,而旧字符串不受影响。 (就像所有字符串操作一样)。因此,每次调用edata后,您都需要用新数据更新.replace()变量

10-06 03:15