js.erb 文件中提供给 RegExp 构造函数的无效标志

Invalid flags supplied to RegExp constructor in js.erb file

我正在尝试使用 jQuery 更改我的 img(s) 悬停时的 src 属性。但是我收到一条错误消息,Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'icon_history'

app.js.erb

 var hover = <%= image_path 'icon_history-h1' %>;

  var unhover = <%= image_path 'icon_history' %>;

  $('img.more').hover(function(){
    console.log(hover);
    $(this).attr('src', hover);
  },
  function(){
    console.log(unhover);
    $(this).attr('src', unhover);
  });

您应该转义字符串并将它们放在引号中:

var hover = '<%= j image_path 'icon_history-h1' %>';

var unhover = '<%= j image_path 'icon_history' %>';

不确定是不是你的情况,但我遇到了类似的问题:

SyntaxError: Invalid flags supplied to RegExp constructor 'u'

我只是通过在我的 Ubuntu 服务器中更新 Node.js 来修复它。 以前的版本是:

$ node -v
v5.1.1

然后我做了:

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
   ... long output
$ sudo apt-get install -y nodejs
   ... shorter output
$ node -v
v6.7.0

再也没有问题了。