Vipin
Vipin

Reputation: 398

Finding an element with jquery with wild card characters

I have a scenario, where i need to find an element using a particular string

("id^='16'")

And my requirement is it should only fetch values with id= '16k','16M' and not values like '16KK' or '16MK'

i.e. it should only fetch for id's which has only one character after search string

Upvotes: 1

Views: 659

Answers (3)

ExceptionLimeCat
ExceptionLimeCat

Reputation: 6400

Use the jquery attribute selector with the filter:

//jQuery requires brackets for Attribute Selector
$("[id^='16']").filter(
function() {
    //Match if not followed by another character
    return this.id.match(/16[a-zA-Z](?![a-zA-Z])/g);
});

Upvotes: 1

Alp
Alp

Reputation: 29739

You can create a jQuery custom filter:

$(document).filter(function() {
    return $(this).attr("id").match( /16[a-z]/i ); 
});

Upvotes: 0

DhruvPathak
DhruvPathak

Reputation: 43245

use filter on returned elements .

$("id^='16'").filter(
function() {
        return this.id.match(/16[a-zA-Z]/);
    });

Upvotes: 2

Related Questions