我想匹配此:试图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