Search code examples
javascriptregexace-editor

Highlight custom token inside quoted string in ace editor


I've lost so much time trying to figure this out, read multiple articles, but without any luck.

What I'm trying to do is highlight a custom token inside a quoted string.

Ex:

    Dim code As String = "SELECT G123C432 FROM table WHERE G321C234 = G87C1"

I've imported a vb.net language grammar to ace, and it's ok, but now I want to highlight G123C432, G321C234 and G87C1.

The vb.net grammar file has this token for quoted string:

    "#quotedString": [{
      token: "string.quoted.double.vbnet",
      regex: /"(?:[^"]|"")*"/
    }]

And I've added this rule:

    "#mlkField": [{
      token: "mlkfield.vbnet",
      regex: /G[\d]+C[\d]+/
    }]

I've tried the regex G[\d]+C[\d]+ but it doesn't work when found inside a String.

Example of an expected final result: editor snippet


Solution

  • use a separate state for the string

    "#quotedString": [{
        token: "string",
        regex: /"/,
        push: [{
            token: "mlkfield.vbnet",
            regex: /G[\d]+C[\d]+/
        }, {
            token: "string",
            regex: /"/,
            next: "pop"
        }, {
            defaultToken: "string"
        }]
    }]