views:

43

answers:

2

Would it be possible to select using a regular expression in jQuery? Something like

$('input[name=^[a-z]+_[1-9].*]')
+2  A: 

You can find a regex selector here (by James Padolsey), called like this in your case:

$('input:regex(name,^[a-z]+_[1-9].*)');
Nick Craver
I love the convenience of custom selectors, but often worry about the performance of them. I haven't checked the code, but I keep meaning to, just to put my mind at ease that they don't put too much extra strain on parsing the whole selector.
Andy E
@Andy - It's definitely a valid concern, just depends how much you're using them and how each browser in-lines the calls, the ideal solution without that info would be declaring the regex before just once.
Nick Craver
+6  A: 

There's nothing built in, but you could use the filter() method to achieve the same goal:

$('input').filter(function () { return /^[a-z]+_[1-9].*/.test(this.name); })
Andy E
+1 very nice solution
Yuval A
+1 - This is cleaner, hope you don't mind - took an extra brace from the question off there
Nick Craver
@Nick: don't mind at all, ta very much :) Teaches me not to just copy and paste blindly lol.
Andy E
That works beautifully.
Josh K