English 中文(简体)
《军法》。 替换案文的容易方式?
原标题:CodeMirror. An easy way to replace text?

I use CodeMirror(5.58.2) for edited 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.

最佳回答

例如......

// 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);
问题回答

我在审判中做得很好:

<!doctype html>
<html>
  <head>
    <title>CodeMirror</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
    <link rel=stylesheet href="https://CodeMirror.net/doc/docs.css">
    <link rel="stylesheet" href="" id="modeFile">
    <link rel="stylesheet" href="colorpicker/addon/codemirror-colorpicker.css" />
    <link rel="stylesheet" href="colorpicker/addon/codemirror-colorpicker.css" />
    <script src="https://CodeMirror.net/addon/hint/anyword-hint.js" id="anyword"></script>
    <link rel="stylesheet" href="https://CodeMirror.net/lib/codemirror.css">
    <link rel="stylesheet" href="https://CodeMirror.net/addon/hint/show-hint.css">
    <link rel="stylesheet" href="https://CodeMirror.net/addon/dialog/dialog.css">
    <link rel="stylesheet" href="https://CodeMirror.net/addon/search/matchesonscrollbar.css">
    <script type="text/javascript" src="colorpicker/addon/codemirror-colorpicker.js"></script>
    <script type="text/javascript" src="colorpicker/addon/codemirror-colorpicker.js"></script>
    <script src="https://CodeMirror.net/lib/codemirror.js"></script>
    <script src="https://CodeMirror.net/addon/edit/closetag.js"></script>
    <script src="https://CodeMirror.net/addon/hint/show-hint.js"></script>
    <script src="https://CodeMirror.net/addon/hint/sql-hint.js"></script>
    <script src="https://CodeMirror.net/addon/mode/loadmode.js"></script>
    <script src="https://CodeMirror.net/mode/meta.js"></script>
    <script src="https://CodeMirror.net/addon/hint/xml-hint.js"></script>
    <script src="https://CodeMirror.net/addon/hint/html-hint.js"></script>
    <script src="https://CodeMirror.net/addon/search/jump-to-line.js"></script>
    <script src="https://CodeMirror.net/addon/hint/javascript-hint.js"></script>
    <script src="https://CodeMirror.net/mode/xml/xml.js"></script>
    <script src="https://CodeMirror.net/mode/javascript/javascript.js"></script>
    <script src="https://CodeMirror.net/mode/css/css.js"></script>
    <script src="https://CodeMirror.net/mode/htmlmixed/htmlmixed.js"></script>
    <script src="https://CodeMirror.net/addon/dialog/dialog.js"></script> 
    <script src="https://CodeMirror.net/addon/search/searchcursor.js"></script>
    <script src="https://CodeMirror.net/addon/search/search.js"></script>
    <script src="https://CodeMirror.net/addon/fold/xml-fold.js"></script>
    <script src="https://CodeMirror.net/addon/scroll/annotatescrollbar.js"></script> 
    <script src="https://CodeMirror.net/addon/search/matchesonscrollbar.js"></script>
    <script src="https://CodeMirror.net/addon/runmode/runmode.js"></script>
    <script src=" https://CodeMirror.net/addon/runmode/colorize.js"></script>
  </head>
  <body>
    <div id="editor"></div>
    <button onclick="find()">find</button>
    <button onclick="replace()">replace</button>
    <button onclick="JTL()"Jump-To-Line</button>
    <button onclick="undo()">undo</button>
    <button onclick="redo()">redo</button>
    <script>
      function find() {
        
        editor.execCommand( find );
      }
      function undo() {
        
        editor.execCommand( undo );
      }
      function redo() {
        
        editor.execCommand( redo );
      }
      function Replace() {
        
        editor.execCommand( replace );
      }
      function JTL() {
        
        editor.execCommand( jumpToLine );
      }
    </script>
    <script>
      var editor = CodeMirror(document.getElementById( editor ),{
          mode:  text/html ,
          matchBrackets: true,
          lineNumbers: true,
        });
    </script>
  </body>
</html>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签