Using "token:" in SimpleMode

In Simple mode the token usage is defined as

token: string | null
An optional token style. Multiple styles can be specified by **separating them with dots or spaces**. When the regex for this rule captures groups, it must capture all of the string (since JS provides no way to find out where a group matched), and this property must hold an array of token styles that has one style for each matched group.

The example given is

// You can match multiple tokens at once. Note that the captured
// groups must span the whole string in this case
{regex: /(function)(\s+)([a-z$][\w$]*)/,
 token: ["keyword", null, "variable-2"]},

The usage description and example don’t quite match, e.g. the example uses a set [] and commas.

In practice when I try

      {regex: /(\[\[)(.*?)(\]\])/, token: ["pm-link", "pm-link-ref", "pm-link"]}

my regex does not match anything, yet the following works

{regex: /(\[\[)(.*?)(\]\])/, token: "pm-link"},

The expression should match


I’d appreciate any suggestions on where to look to sort this out

Firstly, I am not sure why you claim that the description does not match the example. It states that the property must hold an array of token styles when using groups, and that is what the example does.

Your first example works fine when I try it. Are you sure there’s a problem with it?

thanks heaps for the quick reply.
All I’m saying is I found the separating them with dots or spaces unclear, does that mean in fact you can say
token: [“style1 style 2”, “style 3”] ?

Anyway, on to my problem.I’m working with
and trying to add some more specific highlighting. (tested here

After trial and error I can’t get past the two different usages of token:, one of which works, the other of which appears to break the javascript.

In the current test I have the (more selective) regex

{regex: /(\[\[!)(.*?)(\]\])/, token: ["pm-link", "pm-link-ref", "pm-link"]}

Thus when editing the page it does not “break” until you scroll down enough for CodeMirror to action the regex on the line containing [[!Category Name]] (which it matches)

At the same time the following and slightly more general

{regex: /(\[\[)(.*?)(\]\])/, token: "pm-link"}, // changed

works fine.

Thanks again

You can add multiple styles to a single token, by passing a string like "link.string" or "link string". When parsing multiple tokens with a single regexp, you have to pass an array containing the styles for the tokens.

What error are you getting when you use the regex and token you pasted?

Using this markup

{regex: /(\[\[!)([^\]]+)(\]\])/, token: ["pm-link", "pm-link-ref", "pm-link"]}, // category

in Chrome 45.0 with code mirror 5.5 I get the error

Uncaught TypeError: style.replace is not a function
CodeMirror.runMode.callback @ runmode.js:51
CodeMirror.runMode @ runmode.js:66
cmblock @ cmblock.js:8
(anonymous function) @ CodeMirror:826

This works for me (without any error messages) when I paste it into one of
the existing simple modes.