69995

How to read editor text before cursor position to match specific words?

Question:

I am trying to detect when the cursor has moved somewhere immediately after a specific strings ... I can do it only for I have one string , But when I have more than one I cant't matched ... If I have one string like : "color:" then the code to match the word is :

<!-- Create a simple CodeMirror instance --> <link rel="stylesheet" href="lib/codemirror.css"> <script src="lib/codemirror.js"></script> <textarea id="myTextarea"></textarea> <script> var editor = CodeMirror.fromTextArea(myTextarea, { lineNumbers: true }); //Catch cursor change event editor.on('cursorActivity',function(e){ var line = e.doc.getCursor().line, //Cursor line ch = e.doc.getCursor().ch, //Cursor character stringToMatch = "color:", n = stringToMatch.length, stringToTest = e.doc.getLine(line).substr(Math.max(ch - n,0),n); if (stringToTest == stringToMatch) console.log("SUCCESS!!!"); }); </script>

But when I have array of strings like (var array=["one","three","five"]) and I want to match any word in this array I can't do it ... so any body can help I try a lot and failed

NOTE : the code above I take it from : <a href="https://stackoverflow.com/questions/32622128/codemirror-how-to-read-editor-text-before-or-after-cursor-position" rel="nofollow">here</a>

Answer1:

You could use a regular expression for matching one of several words:

var line = e.doc.getCursor().line, //Cursor line ch = e.doc.getCursor().ch, //Cursor character // Select all characters before cursor stringToTest = e.doc.getLine(line).substr(0, ch), // Array with search words: escape characters for use in regular expression: array=["one","three","five"].map( s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') ); // Join words with OR (|) and require they match just before the cursor // (i.e. at end of string: $) if (stringToTest.match(new RegExp('('+array.join('|')+')$'))) console.log("SUCCESS!!!");

Here is a working snippet:

<pre class="snippet-code-js lang-js prettyprint-override">var editor = CodeMirror.fromTextArea(myTextarea, { lineNumbers: true }); //Catch cursor change event editor.on('cursorActivity',function(e){ var line = e.doc.getCursor().line, //Cursor line ch = e.doc.getCursor().ch, //Cursor character // Select all characters before cursor stringToTest = e.doc.getLine(line).substr(0, ch), // Array with search words: escape characters for use in regular expression: array=["one","three","five"] .map( s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') ); // Join words and require they match just before the cursor // (i.e. at end of string: $) if (stringToTest.match(new RegExp('(' + array.join('|') + ')$'))) console.log("SUCCESS!!!"); }); <pre class="snippet-code-html lang-html prettyprint-override"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.19.0/codemirror.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.19.0/codemirror.js"></script> <textarea id="myTextarea"></textarea>

Recommend

  • window.getSelection get the right selection in textarea
  • Prevent user from writing more than N characters in a textarea using Prototype event observers
  • Replacing text inside a textarea box
  • Make the code-mirror block fill the rest of the page in Safari
  • Save all CodeMirrors on page
  • Different font settings for editing code and markdown cells in the Jupyter Notebook
  • WebEngine is not loading files linked in HTML (javafx)
  • Get textarea in codebehind
  • how to resolve OAuthException: (#100)
  • How to get latest version of a artifact on Bintray using JSONP
  • Uncaught TypeError: $(…).select2 is not a function
  • Center align outputs in ipython notebook
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Validate child input components on submit with Vee-Validate and vue js 2
  • How to view images from protected folder with php?
  • Adjust width of select element according to selected option's width
  • error importing numpy
  • Display images in Django
  • Very simple C++ DLL that can be called from .net
  • jQuery .attr() and value
  • How to install a .deb file on a jailbroken iphone programmatically?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • req.body is undefined - nodejs
  • Resize panoramic image to fixed size
  • Modifying destination and filename of gulp-svg-sprite
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Importing jscolor library in angular 2
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • using HTMLImports.whenReady not working in chrome
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection