好吧終於找到了解決方案。您可以使用editor.doc
檢索實際文檔,該文檔可讓您獲取光標行&字符位置。然後,你可以用editor.doc.getLine(n)
檢索所需的線和比較你的子
這裏是我的測試案例:
<!-- 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>
HI @Deftwun有人問你的問題一樣的問題可以幫助呢?此鏈接:http://stackoverflow.com/questions/39917590/how-to-read-editor-text-before-cursor-position-to-match-specific-words – sam