javascript regex: match altered version of first match with only one expression

Posted by theseion on Stack Overflow See other posts from Stack Overflow or by theseion
Published on 2010-04-20T13:49:34Z Indexed on 2010/04/20 13:53 UTC
Read the original article Hit count: 407

Hi there

I'm writing a brush for Alex Gorbatchev's Syntax Highlighter to get highlighting for Smalltalk code. Now, consider the following Smalltalk code:

aCollection do: [ :each | each shout ]

I want to find the block argument ":each" and then match "each" every time it occurrs afterwards (for simplicity, let's say every occurrence an not just inside the brackets). Note that the argument can have any name, e.g. ":myArg".

My attempt to match ":each":

\:([\d\w]+)

This seems to work. The problem is for me to match the occurrences of "each". I thought something like this could work:

\:([\d\w]+)|\1

but the right hand side of the alternation seems to be treated as an independent expression, so backreferencing doesn't work.

So my question is: is it even possible to accomplish what I want in a single expression? Or would I have to use the backreference within a second expression (via another function call)?

Cheers.

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript