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

Invalid flags supplied to RegExp constructor in js.erb file

本文关键字:构造函数 RegExp 标志 无效 erb 文件 js      更新时间:2023-09-26

当img悬停在jQuery上时,我正试图更改它们的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:提供给RegExp构造函数"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

没有更多的问题。