Space and hash (#) causing an error as a coffeescript regex?

The easiest way to express this question is with a small example in repl:

coffee> "hello".split(/: #/) #this is fine
[ 'hello' ]
coffee> "hello".split(/\s#/) #all good here too
[ 'hello' ]
coffee> "hello".split(/ #/)  #wtf??
[stdin]:1:20: error: missing )
"hello".split(/ #/)

      

Why is the last regex not working? from reproducing it seems a bit like there will be an error for any regex match ^ +.*#.*$

Note, i.e. white space at the beginning of the regex. (for example, / foo#bar/

but not /foo bar#baz/

).

Is this a bug in the parser?

(works CoffeeScript version 1.7.1

on Arch Linux

)

+3


source to share


1 answer


Because the coffee lexer is trying to figure out if the first /

division operator or the start of a regex, in which case the flaws are wrong. Here is the relevant code. I'm not sure if this is a bug.

If you put this in a file:

"hallo".match / #/
  1

      

and use coffee -p

, you will see that it is parsed as a division operator. If you add a non-capturing group at the beginning, you end up with an expression that is (almost as it takes up more space and fuzzy regex compilation time) equivalent to what you are aiming for:



"hall #o".match /(?:) #/

      

Edit:

As mu short pointed out , it might be easier to just put a backslash in front of the space.

"hall #o".match /\ #/

      

+4


source







All Articles