Uncaught TypeError: undefined is not a function for window.location.pathname

Uncaught TypeError: undefined is not a function for window.location.pathname

我不断收到以下代码的 Uncaught TypeError: undefined is not a function

var jay = /^(\/gallery\/P[\w\dåäö\/]+)$/;
if(window.location.pathname.test(jay)) {  // It complains about this line
    alert(2);
}

console.log(window.location.pathname) 输出 /gallery/P1290574/%C3%A4ndra。该文件以 UTF-8 编码。在地址栏中显示 /gallery/P1290574/ändra。当我 运行 regexpal.com 上的正则表达式反对 /gallery/P1290574/ändra 时,它就像一个魅力但不是在我的网站上。

我错过了什么?我是否必须更改正则表达式以便它也可以识别 %C3%A4

这应该适合你:

var jay = /^(\/gallery\/P[\w\dåäö\/]+)$/;
if(jay.test(window.location.pathname)) {
    alert(2);
}

你把 .test() 的语法搞反了。它是这样的:

regex.test(variable);