Retrieve line number of string in Ace Editor

我是研究僧i 提交于 2020-01-01 11:39:30

问题


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

回答1:


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
}



回答2:


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



来源:https://stackoverflow.com/questions/18013109/retrieve-line-number-of-string-in-ace-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!