[jQuery] regex operators for the jQuery XPath filter
I'm usually using Perl's Class::XPath and Tree::XPathEngine modules
and there the Perl-style regex matching operators "=~" and "!~" are
available as operators in the XPath filters. What I'm speaking about?
Well, for example selections like: a[@href =~ "^.+(gif|jpe?g|png)$"]
I know that this is neither strict CSS nor XPath syntax, but for being
able to leverage from the full power of regular expressions directly in
the jQuery selectors I found the following small change to the jQuery
core very useful.
At least it satisfied me and my personal Tree::XPathEngine background
and perhaps it is also useful for someone else in the jQuery community.
Find the current patch (against jQuery SVN as of today) appended. Feel
free to do whatever you want with it...
Ralf S. Engelschall
rse@engelschall.com
www.engelschall.com
Index: jquery/src/selector/selector.js
===================================================================
--- jquery/src/selector/selector.js (revision 1611)
+++ jquery/src/selector/selector.js (working copy)
@@ -52,6 +52,8 @@
"@": {
"=": "z==m[4]",
"!=": "z!=m[4]",
+ "=~": "z.match(RegExp(m[4]))!=null",
+ "!~": "z.match(RegExp(m[4]))==null",
"^=": "z&&!z.indexOf(m[4])",
"$=": "z&&z.substr(z.length - m[4].length,m[4].length)==m[4]",
"*=": "z&&z.indexOf(m[4])>=0",
@@ -67,7 +69,7 @@
// The regular expressions that power the parsing engine
parse: [
// Match: [@value='test'], [@foo]
- /^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/,
+ /^\[ *(@)([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
// Match: [div], [div p]
/^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/,