Search code examples
javascriptregexeditorace-editorregex-lookarounds

ace editor non capturing group issue (?:)


All,

I'm trying to create my own mode with the ACE editor . I'm trying to define a really simple syntax highlighting rule:

this.$rules = {
    "start": [
        {
            token : "string", 
            regex : /(?:CONSUME)(MATCH)/,
        }
    ]
};

According to an online Javascript regular expression tester, the regex /(?:CONSUME)(MATCH)/ should see CONSUMEMATCH and return only MATCH. However, ACE highlights all of CONSUMEMATCH with this rule.

Plug the following code into the Ace Mode Creator (http://ace.c9.io/tool/mode_creator.html) to see for yourself.

(In the dropdown menus, I have the Javascript mode and the XCode theme selected. CONSUMEMATCH is my test text, and the following code is the contents of the mode panel.)

define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;

var myHighlightRules = function() {

    this.$rules = {
        "start": [
            {
                token : "string", 
                regex : /(?:CONSUME)(MATCH)/,
            }
        ]
    };
};

oop.inherits(myHighlightRules, TextHighlightRules);
exports.myHighlightRules = myHighlightRules;
});

Solution

  • Since js regexps do not provide a way to determine position of matched group in string, Ace only supports regexps with all matching or all non-matching groups. Try using

    {
        token : ["text", "string"], 
        regex : /(CONSUME)(MATCH)/,
    }
    

    instead.