Martin Bohm
Martin Bohm

Reputation: 35

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

Upvotes: 0

Views: 493

Answers (2)

Stphane
Stphane

Reputation: 3456

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"

Upvotes: 0

Travis Pettry
Travis Pettry

Reputation: 1352

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

Upvotes: 2

Related Questions