0

I want to match these tags:

<div>03-24-2010</div>
<div>04-23-2011</div>

With jquery:

("div:contains([0-9]{2}-[0-9]{2}-[0-9]{4})")

What do I need to do to the regular expression here to make it work (using phpquery - php implementation of jquery)?

JMC
  • 1,700
  • 7
  • 25
  • 34
  • 2
    See [JQuery selector regular expressions](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions) – Matthew Flaschen Apr 01 '11 at 19:39
  • would it be possible to modify the HTML and add a class? that of course would make things easier. or maybe a parent element will always have a class that you could use? – Dave L. Apr 01 '11 at 19:43

2 Answers2

4

Try this:

var divs = $("div").filter(function(){
return /[0-9]{2}-[0-9]{2}-[0-9]{4}/.test($(this).text());
});
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • change `.val()` to `.text()` and that will work, http://jsfiddle.net/markcoleman/3MjzD/ – Mark Coleman Apr 01 '11 at 19:59
  • I'm using phpquery, which is a php implementation of jquery. My mind isn't translating that into the correct php. The part i'm hung up on is the .test. Is that part of jquery? I don't see it in the documentation. – JMC Apr 01 '11 at 20:02
  • @JMC: test is part of Javascript Regular Expression. I am not aware of phpquery.. Let me look at thr phpquery doc to see if I can further update the answer – Chandu Apr 01 '11 at 20:03
2

jQuery doesn't support regex selectors out of the box. You need to use a plugin in or something like http://james.padolsey.com/javascript/regex-selector-for-jquery/ (a filter)

two7s_clash
  • 5,755
  • 1
  • 27
  • 44
  • Not the downvoter, but unfortunately I can't add any extensions to my implementation. – JMC Apr 01 '11 at 19:51
  • @Neal You can use regex in a filter to return something, ok, but there is no regex selecting like JMC is tying to do. `:contains()` takes a simple string only. – two7s_clash Apr 01 '11 at 20:02
  • @Neal, the answer didn't say you can't do regex, it said there's no regex selector. You can undownvote now because it's been edited. – Matthew Flaschen Apr 01 '11 at 20:03
  • @JMC, my second example of is not an extension per se. Just a function you can use. – two7s_clash Apr 01 '11 at 20:05