简体   繁体   English

javascript令牌为“ \\”的正则表达式无效

[英]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: 它可以在https://www.regex101.com/#javascript上运行 ,但是当我定义此连接任务时可以在连接任务中使用grunt:

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. 我收到此错误: Invalid regular expression: /(home?*)([^.]*)$/: Nothing to repeat *)([^.]*) Invalid regular expression: /(home?*)([^.]*)$/: Nothing to repeat ,IDE会在'path'字之间的两个斜杠(\\ path \\)中警告我。

Why i can use those slashes? 为什么我可以使用这些斜线? What can i use to replace those slashes? 我可以用什么代替那些斜线? Thanks very much 非常感谢

The \\ is a special character in javascript so you need to escape it, if you intend to use it. \\是javascript中的特殊字符,因此,如果要使用它,则需要对其进行转义。 You can escape it by adding another \\ . 您可以通过添加另一个\\来对其进行转义。 ex: \\\\ 例如: \\\\

Backslash belongs to "Javascript special characters" 反斜杠属于“ Javascript特殊字符”

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 您可以浏览文档..例如MDN文档

Then search for "JavaScript special characters" 然后搜索“ JavaScript特殊字符”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM