将类添加到正则表达式jQuery [英] add class to regex jquery
本文介绍了将类添加到正则表达式jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个
if( window.location.href == "http://localhost:3000/categories") {
$("#container ul #all_categories a.categories-menu").css("font-weight","bold");
}
});
但是我有一个网址:
http://localhost:3000/username/boards
用户名是动态用户名,例如
http://localhost:3000/michael-21/boards
http://localhost:3000/Richard_10/boards
http://localhost:3000/Mary.50/boards
并为每个用户进行更改.
and change for each user.
如何使用最后一个URL来实现相同的功能?
How can do the same functionality but with this last url?
谢谢
推荐答案
您不需要RegEx.以下方法更容易维护,也更容易维护(将页面移至生产环境时无需更改代码中的URL):
You don't need a RegEx. The following method is easier, and is also easier to maintain (you don't have to change the URL in the code when moving the page to the production environment):
location.pathname.indexOf('/boards', 1) !== -1
上一个也将匹配/me/not/boards
.如果您不希望这样做:
The previous would also match /me/not/boards
. If you don't want that:
var index = location.pathname.indexOf('/boards', 1);
index !== -1 && location.pathname.lastIndexOf('/', index-1) === 0
最后,RegEx方法:
Finally, the RegEx method:
/^\/[^/]+\/boards/.test(location.pathname)
实施示例
if( /^\/[^/]+\/boards/.test(location.pathname) ) {
$("#container ul #all_categories a.categories-menu").css("font-weight","bold");
}
});
这篇关于将类添加到正则表达式jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文