[TxMt] Better JavaScript functions

Allan Odgaard allan at macromates.com
Mon Sep 19 13:14:37 UTC 2005


On 19/09/2005, at 10.53, Andreas Wahlin wrote:

> I then had to split it up into two
> [...]
> is there a better way?

You can use alternation: (foo|bar). So combined it'd be:

     match = "^\\s*(?:this\\.|(var\\b)?\\s*)([a-zA-Z_]\\w*)\\s*=\\s* 
(function)\\s*\\(([^)]*)\\)";

I used the (?:foo|bar) form to avoid making it a capture. It is btw  
possible to prefix the expression with (?x) to enter “extended’ mode  
where whitespace and comments are ignored, so we can pretty print it:

     match = "(?x)
         ^\\s*                # begin-of-line + leading space
         (?: this\\.          # literal this.
           | (var\\s+)        # -> or literal var (capture #1)
           |                  # -> or nothing
         )
         ([a-zA-Z_]\\w*)\\s*  # actual function name (capture #2)
         =\\s*                # literal =
         (function)\\s*       # function keyword (capture #3)
         \\(([^)]*)\\)        # the parameters (capture #4)
     ";

Don't know if you find that more readable or not…




More information about the textmate mailing list