javascript 语法错误:无效的正则表达式:缺少 /

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23220850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:34:04  来源:igfitidea点击:

SyntaxError: Invalid regular expression: missing /

javascriptregex

提问by Joshlo

According to http://www.regexr.com/38o5dmy reqex seems to work, but when I implement it into my javascript

根据http://www.regexr.com/38o5d我的 reqex 似乎可以工作,但是当我将它实现到我的 javascript 中时

var prefix = hash.replace(/\/|#/g, '');

var prefix = hash.replace(/\/|#/g, '');

I'll get the following error: SyntaxError: Invalid regular expression: missing /

我会收到以下错误:SyntaxError: Invalid regular expression: missing /

回答by RichieHindle

Psychic debugging: Your code isn't in a plain JavaScript or HTML file, but is being printed from a scripting language in which #is a comment character. The #/g, '');piece is being treated as a comment in your script.

心理调试:您的代码不是在普通的 JavaScript 或 HTML 文件中,而是从#带有注释字符的脚本语言中打印出来的。该#/g, '');片段被视为脚本中的注释。

You need to add some quotes to ensure that the whole line gets printed.

您需要添加一些引号以确保打印整行。