Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript: why does "str".replace(/ /g, "") give coffeescript compile error?

The offending line

"str".replace(/ /g, "")

gives

Error: In orders.js.erb.coffee, Parse error on line 463: Unexpected 'MATH'
    at Object.parseError (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:466:11)
    at Object.parse (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/parser.js:542:22)
    at Object.compile (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:38:22)
    at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:149:33
    at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:115:19
    at [object Object].<anonymous> (fs.js:107:5)
    at [object Object].emit (events.js:61:17)
    at afterRead (fs.js:878:12)
    at wrapper (fs.js:245:17)

Is this a bug in the coffeescript compiler or am I missing something?

like image 700
James Avatar asked Apr 10 '12 23:04

James


People also ask

Should you use CoffeeScript?

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.


1 Answers

Escape the first whitespace inside of the regexp

"str".replace(/\ /g, "")

compiles just fine.

like image 162
James Avatar answered Oct 22 '22 04:10

James