为什么javascript会替换抛出错误

时间:2014-09-30 22:22:31

标签: javascript regex syntax-error

我在第一个逗号处继续收到此错误“SyntaxError:syntax error”,我应该更改什么才能使其正常工作?

if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'') && location.hostname == this.hostname)

3 个答案:

答案 0 :(得分:1)

您需要在正则表达式中转义正斜杠

if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)

答案 1 :(得分:1)

这是双斜线,

if (location.pathname.replace(/^/,'') == this.pathname.replace(/^/,'') && location.hostname == this.hostname)

答案 2 :(得分:0)

使用RegExp构造函数

var regexp = new RegExp("^/", "");
if (location.pathname.replace(regexp,'') == this.pathname.replace(regexp,'') && location.hostname == this.hostname)

或转义字符/

if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)