在 Ace Editor 中检索字符串的行号 [英] Retrieve line number of string in Ace Editor

查看:48
本文介绍了在 Ace Editor 中检索字符串的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索 ace 编辑器中显示的文本中给定字符串的行号.

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

  1. 示例:搜索foo"
  2. 返回:[ 4, 5 ]
  3. 条件:ace 编辑器内容的第 4 行和第 5 行包含foo"字符串

推荐答案

遍历所有行并检查 indexOf

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
}

这篇关于在 Ace Editor 中检索字符串的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆