Javascript 未捕获类型错误:未定义不是window.location.pathname的函数
我不断得到Javascript 未捕获类型错误:未定义不是window.location.pathname的函数,javascript,regex,typeerror,undefined-function,Javascript,Regex,Typeerror,Undefined 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编码。地址栏上写着/gal
未捕获类型错误:未定义不是以下代码的函数
:
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/228 ndra
。当我在/gallery/P1290574/ändra
上运行正则表达式时,它就像一个符咒,但在我的网站上却不起作用
我错过了什么?我是否必须更改正则表达式,以便它也可以识别
%C3%A4
var jay = /^(\/gallery\/P[\w\dåäö\/]+)$/;
if(jay.test(window.location.pathname)) {
alert(2);
}
您已经掌握了.test()
的语法。事情是这样的:
regex.test(variable);
你错过了
.test()
不是一个字符串方法。所以我必须为window.location.pathname
使用一个变量@Juhana的意思是它应该是jay.test(window.location.pathname)
一般来说,“未定义的不是函数”字面意思是(在本例中).test
未定义(即不存在)你试着把它当作函数来调用。如果方法存在,但参数错误,则永远无法得到该错误。此外,您也永远无法通过将值分配给另一个变量来修复“undefined is not a function”错误,因为新变量的方法与原始变量完全相同。