获取页面上与 url 字符串匹配的所有链接
我目前正在使用此代码(与 Mootools 一起)在 #subnav
div 中构建包含特定 url 字符串的所有锚点的数组:
$('subnav').getElements( 'a[href*=/'+href+']')
问题是,如果我正在寻找将锚点与 a 相匹配的 work.aspx?subsection=24&project=1
work.aspx?subsection=24&project=15
的 URL。
我怎样才能防止这种情况发生?
I'm currently using this code (along with Mootools) to build an array of all anchors in the #subnav
div that contain a specific url string:
$('subnav').getElements('a[href*=/'+href+']')
Problem is if I'm looking for work.aspx?subsection=24&project=1
that will match anchors with a URL of work.aspx?subsection=24&project=15
.
How can I prevent that from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的唯一解决方案是使用正则表达式;像这样的东西:
该正则表达式将匹配project=1,然后匹配
&
或字符串的结尾。它使用 MooTool 的(或本机浏览器的)Array.filter
来删除所有不匹配项。这不是最优雅的解决方案,特别是如果您动态使用它,那么您必须编写一些代码来创建新的正则表达式。The only solution I can think of is to use regexes; something like this:
That regex will match project=1 and then either a
&
or the end of the string. It uses MooTool's (or the native browser's)Array.filter
to remove all non-matches. It isn't the most elegant solution, especially if you are using this dynamically, since then you'd have to write some code to create a new regex.我要回答我自己的问题。就像将
*
更改为$
一样简单。$('subnav').getElements('a[href$=/'+href+']')
Going to answer my own question. It was as simple as changing the
*
to a$
.$('subnav').getElements('a[href$=/'+href+']')
听起来这应该通过 301 或 302 重定向来更优雅地处理......
Sounds like this should be handled more gracefully by a 301 or 302 redirect...