[英]Why is the javascript replace throwing an error
我在第一個逗號不斷收到此錯誤“ SyntaxError:語法錯誤”,我應該更改使其起作用嗎?
if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'') && location.hostname == this.hostname)
您需要在正則表達式中轉義正斜杠
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
是雙斜線,
if (location.pathname.replace(/^/,'') == this.pathname.replace(/^/,'') && location.hostname == this.hostname)
使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.