首页 > 解决方案 > 代码镜像。替换文本的简单方法?

问题描述

我使用CodeMirror (5.58.2)来编辑文本。

new_cm = CodeMirror.fromTextArea(textarea_obj, param);

但是在 textarea 中,我可以轻松地替换文本,只需这样做obj.value = obj.value.replace( /123/g, '3210'); 我可以在 CodeMirror 中这样做吗?对用户没有任何接口请求。只是一个简单的“Make Replace”按钮和带有正则表达式模式的代码。

标签: javascriptcodemirror

解决方案


这是一个例子......

// start the editor instance  
const new_cm = CodeMirror.fromTextArea(textarea_obj, param);

// get the entire editor text from CodeMirror editor  
let text = new_cm.getValue();

// edit the text, for example  
text = text.replace(/abc/g, '');

// set the text back to the editor  
new_cm.setValue(text);

推荐阅读