1
我使用以下JavaScript與正則表達式來測試url字符串。具有查詢字符串的URL的正則表達式
var url = window.location.pathname;
// create regexp to match current url pathname and remove trailing slash if
// present as it could collide with the link in navigation in case
// trailing slash wasn't present there
urlRegExp = new RegExp(url == '/' ? window.location.origin + '/?$' : url.replace(/\/$/, ''));
// now grab every link from the navigation
$('.page-sidebar a').each(function() {
// and test its normalized href against the url pathname regexp
if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
$(this).closest("li").addClass("active");
}
});
但是,這個正則表達式不包括查詢字符串。我怎樣才能做到這一點?
初步認識問題http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links/37687 – shakib
HTTP玩: //stackoverflow.com/about – Xotic750