【发布时间】:2011-05-17 23:28:52
【问题描述】:
我的代码中有这个变量:
var href = $(this).attr('href'),
str = href.split('/').slice(-2,-1)[0];
如何检查“images”、“images-2”还是“images-3” 匹配'href'字符串,然后做一些事情..
【问题讨论】:
标签: javascript jquery string href
我的代码中有这个变量:
var href = $(this).attr('href'),
str = href.split('/').slice(-2,-1)[0];
如何检查“images”、“images-2”还是“images-3” 匹配'href'字符串,然后做一些事情..
【问题讨论】:
标签: javascript jquery string href
if href.match(/images(-[23])?/){
doStuf();
}
应该可以解决问题。
【讨论】:
看看这个: http://api.jquery.com/category/selectors/
你可以有类似的东西: $('[href^="images"]')
编辑: 如果您愿意,您也可以制作自己的选择器。 http://dpatrickcaldwell.blogspot.com/2010/03/custom-jquery-selector-for-external-and.html
James Padolsey 已经写了一篇适合你的文章。 http://james.padolsey.com/javascript/regex-selector-for-jquery/
【讨论】:
~= images 仍会捕获图像 12,严格阅读 OP 会认为这是不可接受的。