Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve line number of string in Ace Editor

I'm trying to retrieve the line number of a given string in the text displayed in the ace editor.

  1. Example: searching for "foo"
  2. Return: [ 4, 5 ]
  3. Condition: Line 4 and 5 in the content of the ace editor contains the "foo" string
like image 297
Coxer Avatar asked Aug 02 '13 09:08

Coxer


1 Answers

Iterate over all lines and check indexOf

function findFooLineNumbers(editor, foo) {
    var lines = editor.session.doc.getAllLines()
    var fooLineNumbers = []
    for (var i = 0, l = lines.length; i < l; i++) {
        if (lines[i].indexOf(foo) != -1)
           fooLineNumbers.push(i)
    }
    return fooLineNumbers
}
like image 71
a user Avatar answered Nov 12 '22 23:11

a user