Search code examples
javascriptregexgrunt-contrib-connectconnect-modrewrite

javascript Invalid regular expression with token '\'


i created the following regex for finding a url matching a word in the complete path and that does't contain the point character.

(\path\?*)([^.]*)$

It works on https://www.regex101.com/#javascript, but on grunt in the connect task when i define this connect task:

middleware: function(connect, options) {
 var middlewares = [];

 middlewares.push(modRewrite(["(\path\?*)([^.]*)$ /home.html [L]"])); 
    options.base.forEach(function(base) {
      middlewares.push(connect.static(base));
    });
    return middlewares;
  }

i got this error: Invalid regular expression: /(home?*)([^.]*)$/: Nothing to repeat and the IDE warn me in the two slash (\path\ ) between the 'path ' word.

Why i can use those slashes? What can i use to replace those slashes? Thanks very much


Solution

  • The \ is a special character in javascript so you need to escape it, if you intend to use it. You can escape it by adding another \. ex: \\