-1

I am trying to write a very generic editor, and wanted to highlight any known keywords, no context awareness required.

I created the following regex

    var commonAttributes = ["var", "val", "let", "if", "else", "export", "import", "return", "static", "fun", "function", "func", "class", "open", "new", "as", "where", "select", "delete", "add", "limit", "update", "insert"]
    let standalonePrefix = "(?<=[\\s]|^|[\\(,:])"
    let standaloneSuffix = "(?=[\\s\\?\\!,:\\)\\();]|$)"

and the following state.

    {
          regex: new RegExp(standalonePrefix+"("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
          token: "keyword"
    },

I understand that to match at line beginning , I would have to use sol: true, as ^ has no meaning in our context. But this causes problems for me. without sol: true, writing

let leaflet let

will highlight all lets. with sol: true, only first let will match

let leaflet let

My desired outcome is that i get,

let leaflet let

How can I do so?

4

1 回答 1

0

由于我找不到任何可以解决此问题的方法,因此我最终使用了一种解决方法。

我从前缀中删除了 ^,并使用 ^ 和 sol: true 创建了另一个状态。

let standalonePrefix = "(?<=[\\s]|[\\(,:])"
{
        regex: new RegExp(standalonePrefix+"("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
        token: "keyword"
},
{
        regex: new RegExp("(?:^)("+commonAttributes.join("|")+")"+standaloneSuffix, "i"),
        sol: true,
        token: "keyword"
},
于 2020-07-28T23:58:19.527 回答