如何根据codemirror中的子字符串获取行号?

1 javascript codemirror

我需要在具有 codemirror 插件的 textarea 中获取特定字符串的行号。

var editor= CodeMirror.fromTextArea(document.getElementById("refstyle");  

var doc = editor.getDoc();
var cursor = doc.getCursor(); // gets the line number in the cursor position
var line = doc.getLine(cursor.line); // get the line contents 
editor.getSelection(line number)   //it will select the text based on line number
Run Code Online (Sandbox Code Playgroud)

那么,如何根据字符串获取行号(我希望像 doc.getLine(string) 或 doc.getSelection(String))

是否有任何函数可以根据字符串获取行号或根据文本区域中的行号获取选择的搜索字符串?请帮忙!

Mar*_*ijn 5

要在 CodeMirror 实例中搜索,请使用searchcursor插件。它会给你匹配的行/字符位置。