Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

ACE Editor multiline regex

I'm using ACE Editor to syntax highlight my website's BBCode system, and on the whole it's working well. The only thing that isn't is our equivalent to multiline comments:

[nobbcode]
    I am a demo of [b]BBCode[/b].
    Anything in here is shown as plain text, with code left intact,
    allowing people to show how it works.
[/nobbcode]

The rule for this is:

{
    token:[
        "meta.tag.punctuation.tag-open.xml",
        "meta.tag.tag-name.xml",
        "meta.tag.punctuation.tag-close.xml",
        "comment",
        "meta.tag.punctuation.end-tag-open.xml",
        "meta.tag.tag-name.xml",
        "meta.tag.punctuation.tag-close.xml"
    ],
    regex:/(\[)(nobbcode)(\])([\s\S]*?)(\[\/)(nobbcode)(\])/,
    caseInsensitive:true
},

And it works great in an example like this:

You can use [nobbcode][b]...[/b][/nobbcode] to designate bold text.

where the match is on a single line, but it doesn't seem to like multiline text.

Does ACE not support multi-line regex, and if so should I instead break it down into "start comment, comment, end comment" parts?

Upvotes: 2

Views: 1688

Answers (2)

dkretz
dkretz

Reputation: 37645

Perhaps the best workaround is to use js to concatenate the array of lines (eg array.join(\n)) delimited by a newline, run your regex on that, split it back out on the newlines, and replace the array.

Probably creates scaling issues at some point, however.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

Thanks to @Thomas' comment, I learned that ACE parses line-by-line, and therefore multiline regexes will not work.

I fixed my issue with the following syntax rule:

{
    token:[
        "meta.tag.punctuation.tag-open.xml",
        "meta.tag.tag-name.xml",
        "meta.tag.punctuation.tag-close.xml"
    ],
    regex:/(\[)(nobbcode)(\])/,
    caseInsensitive:true,
    push:[
        {
            token:[
                "meta.tag.punctuation.end-tag-open.xml",
                "meta.tag.tag-name.xml",
                "meta.tag.punctuation.tag-close.xml"
            ],
            regex:/(\[\/)(nobbcode)(\])/,
            caseInsensitive:true,
            next:"pop"
        },
        {defaultToken:"comment"}
    ]
},

This essentially breaks it down into start-middle-end, applying the "comment" token to the middle part with defaultToken.

I just wish ACE were documented better...

Upvotes: 2

Related Questions