javascriptcodemirror

CodeMirror. An easy way to replace text?


I use CodeMirror(5.58.2) for editing text.

new_cm = CodeMirror.fromTextArea(textarea_obj, param);

But in the textarea, I can easy replace text, just do this obj.value = obj.value.replace( /123/g, '3210'); What can I do smth like that in CodeMirror? Without any interface requests for user. Just a simple "Make Replace" button and code with a regexp pattern.


Solution

  • Here is an example ...

    // 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);