Coxer
Coxer

Reputation: 1704

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

Upvotes: 7

Views: 3991

Answers (4)

Oussama Khoubrane
Oussama Khoubrane

Reputation: 9

You can simply use

editor.session.getLength()

Upvotes: 0

Greg Greansky
Greg Greansky

Reputation: 85

In short:

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

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

Upvotes: 0

a user
a user

Reputation: 24104

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
}

Upvotes: 6

Ivan
Ivan

Reputation: 5248

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

Upvotes: -1

Related Questions