我想匹配此:試圖URL路徑匹配的正則表達式
/Wedding
到/Wedding/Areas
/Wedding
是相匹配的詞。
我想我並沒有正確逃避角色。
var pattern = new RegExp("^/" + href + "*$");
這是怎麼了,我形成動態測試。任何幫助將受到歡迎。
我認爲這將有助於更多。我試圖匹配網址爲當前頁面和子頁面添加一個類
`var activePage = window.location.pathname; $( 「#NAV利A」)各(函數(){ VAR HREF = $(本).attr(的 「href」);
// var pattern = new RegExp("^/" + href + "/\\.*$");
var pattern = new RegExp("^/" + href + ".*$")
if (pattern.test(activePage)) {
$(this).addClass("active");
}
});
`
編輯:
這是我找到替代於正則表達式的解決方案。任何人都可以想出另一種方式與正則表達式來解決這個問題?
var activePage = window.location.pathname; $("#nav li a").each(function() { var href = $(this).attr("href"); if (window.location.pathname.indexOf(href) == 0) { if (href != "/") { $(this).addClass("active"); } if (href == "/" && activePage == href) { $(this).addClass("active"); } } });
你爲什麼要逃脫點? – dldnh 2012-03-30 16:02:04
逃避'.'字面意思地匹配點。它應該是'var pattern = new RegExp(「^ /」+ href +「。* $」)' – 2012-03-30 16:02:31
哎呀,對不起。一天結束。需要睡覺;) – 2012-03-30 16:04:35