0

I am trying to only select links with "example.com/foo/12345/bar" pattern where they can be any number of digits. But jquery doesn't seem to accept \d for digits. Any other suggestions?

$('a[href*="example.com\/foo\/\d+\/bar"]').hover(function(){});
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
bigbucky
  • 407
  • 6
  • 20
  • 3
    jQuery doesn't accept regex there, but you can use `filter`, then `regex.test(this.href)` in the callback. – elclanrs Jan 10 '15 at 09:48
  • The attribute selector does not accept RegEx expressions. You would either have to fetch the ID and perform the matching iteratively, or use a custom pseudo-class: http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions – Terry Jan 10 '15 at 09:49
  • You might try combination of multiple attribute selector, `$('a[href*="example.com/foo"][href$="bar"]')` – Satpal Jan 10 '15 at 09:50

1 Answers1

1

jQuery doesn't accept regex there, but you can use filter:

$('a').filter(function() {
  return /example.com\/foo\/\d+\/bar/.test(this.href)
}).hover(function() {

})
elclanrs
  • 92,861
  • 21
  • 134
  • 171