ckeditor替换word如何添加更多?

时间:2015-07-13 05:04:10

标签: javascript ckeditor

我可以使用此正则表达式更改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);

1 个答案:

答案 0 :(得分:1)

您的代码中存在错误

这是固定版本

      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()返回一个NEW字符串并使旧字符串不受影响。 (就像任何字符串操作btw)。因此,您需要在每次调用edata

后使用新数据更新.replace()变量