jQuery Select # id with word as prefix and counter as suffix

javascript, jquery

Solution

First thoughts, which seems to work well:

$('div[id^="my"]').filter(
    function(){
        return this.id.match(/\d+$/);
    });

JS Fiddle demo.

The above selects all `div` elements whose `id` starts with the value `my`, and then filters the returned elements to those whose `id` also ends with numeric characters.

References:

- attribute-starts-with selector.

- `filter()`.

- Regular Expressions, at Mozilla Developer Network.

Problem

Is there a way to select all id's with jQuery with a prefix "my" and a suffix "0-9". Something like these $("#my$1-4") or is it just possible with a loop ? ``` <div id="my1"/> <div id="my2"/> <div id="my3"/> <div id="my4"/> <div id="my5"/> ```

Original source

Related problems