我试图在文本中查找单词,如果确实存在,则将其替换。但是问题在于我猜想正则表达式是因为它看到像“ hello”这样的单词,例如“%!hello!%”。而且它也找不到文本中的单词。有什么建议么 ?
scriptPanel.setValue = function (oldValue,newValue) {
var re = new RegExp("\\b" + oldValue + "\\b","g");
var patt= sincapp.codeEditor.getValue().test(re);
if(patt){
var newText = sincapp.codeEditor.getValue().replace(re,oldValue);
sincapp.codeEditor.setValue(newText);
}
};
最佳答案
您将用用于查找它的相同变量替换它。另外,更换之前不需要进行测试,因为这只是多余的操作。
scriptPanel.setValue = function(oldValue, newValue) {
var re = new RegExp("\\b" + oldValue + "\\b", "g");
var newText = sincapp.codeEditor.getValue().replace(re, newValue); // new value!!!
sincapp.codeEditor.setValue(newText);
};