Heh, I did something just like that for a project I'm working on, but I used /= instead. I had thought about using =~, but ~= is an official CSS selector (which jQuery has chosen not to support because of a supposed lack of real-world usefulness) and I didn't want the two to get confused. / is JavaScript's regexp delimiter, so I thought it would be more recognizable that way. Also, mine doesn't include negation (because I didn't need it). Here's my code:
/** Custom attribute selector to allow testing attribute values against * regular expressions * * Do not include delimiters in your regular expression. If you need to * use \, be aware that you have to use \\ instead because of the way * string escaping is done in JavaScript * * @example $(':[EMAIL PROTECTED]/="bday\\[\\w+\\]"]') * @before [<select name="bday[month]">, <select name="bday[day]">, <input name="bday[year]">, <input name="foobar">] * @result [<select name="bday[month]">, <select name="bday[day]">, <input name="bday[year]">] * @desc Test for a form input whose "name" attribute matches the regular expression bday\[\w+\] * * @name /= * @type string * @cat Plugins/HValidator */ $.expr["@"]["/="] = "(new RegExp(m[4])).test(z)"; // Parser to enable detection of things like [EMAIL PROTECTED]/="<insert regexp here>"] $.parse.splice(0, 0, /^\[ *(@)([a-z0-9_-]*) *(\/=) *('?"?)(.*?)\4 *\]/i); On 4/2/07, Ralf S. Engelschall <[EMAIL PROTECTED]> wrote:
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: [EMAIL PROTECTED] =~ "^.+(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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED]'test'], [EMAIL PROTECTED] - /^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/, + /^\[ *(@)([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/, // Match: [div], [div p] /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/,
-- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com