GregS7
GregS7

Reputation: 43

Using regex to find elements based on partial contents of attributes

How do I go about using regex to match any anchor whos href starts with "#t_row_" ? For example, one of the anchors looks like: <a href="#t_row_234567890">Link</a>

Currently I have:

$('a[href*=#]')each(function() {

Cheers

Upvotes: 2

Views: 971

Answers (3)

Siedrix
Siedrix

Reputation: 641

What you need is

$('a[href^="#t_row_"]')

http://api.jquery.com/attribute-starts-with-selector/

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318518

No need to use a regex. $('a[href^="#t_row_"]') will do the job.

http://api.jquery.com/attribute-starts-with-selector/

Upvotes: 2

Phrogz
Phrogz

Reputation: 303251

For this exact case, use the jQuery Attribute Starts With selector:

$('a[href^="#t_row"]')

Alternatively, in a more general case, you can filter() any set of elements based on arbitrary JS code. In this case it would be:

$('a').filter(function(i){ return /^#t_row/.test(this.href); });

Upvotes: 2

Related Questions