0

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

arco444
  • 22,002
  • 12
  • 63
  • 67

2 Answers2

2

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: \\

Travis Pettry
  • 1,220
  • 1
  • 14
  • 35
0

Backslash belongs to "Javascript special characters"

Because you use backslashes inside a double quoted string, you have to write each one twice to tell the parser that you actually want to ouput a backslah. This is mandatory inside double quoted and single quoted strings.

You might browse the documentation .. for instance MDN documentation

Then search for "JavaScript special characters"

Stphane
  • 3,368
  • 5
  • 32
  • 47