jQuery Regex Question - Simple Match
How can I match something using jquery that is set up as such:
- id="intersection_resources_4_5" // hit
- id="intersection_conten开发者_如何学JAVAt_6_5" // hit
- id="intersection_content_4_3" // not
I want to do something like:
$("div:regex(id, intersection_*_*_5)").each(function(){
$(this).click();
});
Is this doable? Assuming, the * is a wildcard, which I cannot seem to get to work.
If you don't want a plugin, and you don't want to create your own selector filter, do this:
$("div[id^=intersection]").filter(function() {
var header = this.id.substr(13);
var regex = new RegExp('_[a-zA-Z]+_\\d+_' + header + '$');
return regex.test(this.id);
}).click();
It would be best if your <div>
elements had a class in order to improve performance.
Something like:
$("div.intersection[id^=intersection]")...
With filter, and a proper regex, this should work
$("div:regex(id, ^intersection_\d_\d_5$)").each(function(){
$(this).click();
});
EDIT:
Try this regex instead ^intersection_[\w-[0-9]]+_\d+_\d+$
You can replace parts between two underscores with the specific word/number if you know what it is.
There is regex selector for jQuery:
http://james.padolsey.com/javascript/regex-selector-for-jquery/
That is used like this:
$(':regex(id,^[aeiou])');
Use filter method with custom function. Something like this:
var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
return $(this).attr("id").match(r);
}).click();
精彩评论