6

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
4

3 回答 3

7

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
}
于 2013-08-03T08:19:46.597 回答
0

In short:

var found = editor.$search.find(editor.getSession());

More on this on https://www.debugcn.com/en/article/12331736.html

于 2021-09-12T20:29:11.250 回答
-1

You left too little information and you can not expect a great help

If you want to return more information at the same time u need Array

var number = new Number(5) // Single number. he will return just 5

You can try somthing like this to see how to return array

function test() {
    var IDs = new Array();
        IDs['s'] = "1 342 364,586";
        IDs['g'] = "123 324 646 876";

        for (var i = 0; i <= IDs.lenght; i ++ ) {
             // do somthing  
        }
    return IDs;
}

To check if return is realy nubmer use Number.NaN

于 2013-08-02T09:33:53.300 回答