1

I got divs with IDs like that:

<div id="drop_12_213_1">
<div id="drop_12_213_2">
<div id="drop_12_213_3">
<div id="drop_13_213_4">

The only thing I know is drop and the last number(e.g. 1).

How can I select it whit jQuery?

$('div[id^=drop_*_*_1]') is not working?

What am I doing wrong?

TIA frgtv10

frgtv10
  • 5,300
  • 4
  • 30
  • 46

1 Answers1

3

jQuery doesn't support regular expressions as selectors by default. There are some filters that could do that.

What you can do instead is use the jQuery filter() method.

$('div').filter(function() {
    return this.id.match(/^drop_\d+_\d+_1$/);
}).html("match");

This will basically check all the divs in your document and use the filter() method to see if it should match or not. I imagine this will not be the fastest way of doing, you could try to restrict the search as much as possible (e.g. $('div', container_of_divs)), but by the looks of what you're trying to parse, it seems result is more important than speed.

As a note, your regular expression is wrong too.

drop_*_*_1 translates to something like:

  • drop followed by
  • _ zero or more times,
  • _ zero or more times,
  • _1

Which would match something like drop_1 or drop__1 or drop___..._1.

Links:

Community
  • 1
  • 1
Cristi Mihai
  • 2,505
  • 1
  • 19
  • 20
  • Thanks for your help. Is it possible to add the number as an variable in the `match(/.../)` part? – frgtv10 Aug 28 '12 at 13:03
  • You would want to build that regular expression dynamically in that case. I did something like that here: http://jsfiddle.net/3fXke/1/ – Cristi Mihai Aug 28 '12 at 13:12