http://james.padolsey.com/snippets/regex-selector-for-jquery/

A while ago I published an article explaining the utter awesomeness of extending jQuery’s filter selectors. Building on that here’s something new; a regular expression selector. jQuery’s current attribute selectors (CSS3) do allow basic regex notation but no where near as much as this:

:regex

jQuery.expr[':'].regex = function(elem, index, match) {
var matchParams = match[3].split(','),
validLabels = /^(data|css):/,
attr = {
method: matchParams[0].match(validLabels) ?
matchParams[0].split(':')[0] : 'attr',
property: matchParams.shift().replace(validLabels,'')
},
regexFlags = 'ig',
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
return regex.test(jQuery(elem)[attr.method](attr.property));
}

Usage

It’s pretty simple to use, you need to pass an attribute and a regular expression to match against. The regular expression must be in non-literal notation; so replace all backslashes with two backslashes (e.g. ^\w+$ -> ^\\w+$).

// Select all elements with an ID starting a vowel:
$(':regex(id,^[aeiou])');
 
// Select all DIVs with classes that contain numbers:
$('div:regex(class,[0-9])');
 
// Select all SCRIPT tags with a SRC containing jQuery:
$('script:regex(src,jQuery)');
 
// Yes, I know the last example could be achieved with
// CSS3 attribute selectors; it's just an example...

Note: All searches are case insensitive; you can change this by removing the ‘i’ flag in the plugin.

This plugin also allows you to query CSS styles with regular expressions, for example:

// Select all elements with a width between 100 and 300:
$(':regex(css:width, ^[1-3]\\d{2}px$)');
 
// Select all NON block-level DIVs:
$('div:not(:regex(css:display, ^block$))');

Additionally it allows you to query data strings added to elements via jQuery’s ‘data’ method:

// Add data property to all images (just an example);
$('img').each(function(){
$(this).data('extension', $(this)[0].src.match(/.(.{1,4})$/)[1]);
});
 
// Select all images with PNG or JPG extensions:
$('img:regex(data:extension, png|jpg)');

Have fun!

jQuery 正则选择器的更多相关文章

  1. jQuery的选择器+实例

    返回目录 jQuery的冒号选择器 表单  :input :text :password :radio :checkbox :submit :image :reset :button :file :h ...

  2. jQuery的选择器总结

    jQuery的选择器 不会返回undefined或null 基本选择器 id选择器:$('#id') element选择器:$('elem') class选择器:$('.class') 通配符选择器: ...

  3. jQuery 的选择器常用的元素查找方法

    jQuery 的选择器常用的元素查找方法 基本选择器: $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myE ...

  4. HTML 学习笔记 JQuery(选择器)

    学习前端也有一段时间了,今天终于进入到JQuery阶段了,对于新手来讲,JQuery的选择器类型之多 功能之强大实在不是一天两天能够记得完的.现在,就采用边学边记录的方式.以后要是忘了的话,也有一个地 ...

  5. jQuery的选择器中的通配符总结

    1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']&quo ...

  6. JQuery 层次选择器

    <!DOCTYPE HTML> <html> <head> <title> 使用jQuery层次选择器 </title> <scrip ...

  7. jQuery过滤选择器

    //基本过滤器$('li:first').css('background','#ccc');//第一个元素$('li:last').css('background','red');//最后一个元素$( ...

  8. jquery相对选择器,又叫context选择器,上下文选择器;find()与children()区别

    jquery相对选择器有两个参数,jQuery函数的第二个参数可以指定DOM元素的搜索范围(即以第二个参数指定的内容为容器查找指定元素). 第二个参数的不同的类型,对应的用法如下表所示. 类型 用法 ...

  9. jQuery之选择器

    jQuery元素选择器和属性选择器允许您通过标签名.属性名或内容对 HTML 元素进行选择和操作,而在 HTML DOM中,选择器可以对DOM元素组或单个DOM 节点进行操作.通俗点说,选择器的作用就 ...

随机推荐

  1. Stream 和 byte[] 之间的转换

    Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...

  2. delphi中的各种文件类型介绍【转】

    1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...

  3. python的with...as用法

    with...as叫做上下文管理器,作用是进入一个对象的作用域和离开时,可以执行执行一定的操作.这个操作是可以自己 设定的. 写个例子学习一下: class test(): def __init__( ...

  4. 关于ios使用jquery的on,委托事件失效

    $('.parents').on("click",'.child',function(){}); 类似上面这种,在ios上点击"child"元素不会起作用,解决 ...

  5. 反转链表,时间复杂度O(n),空间复杂度O(1)

    原理:使用三个指针,p,q指向交换的元素,r指向后续元素 代码如下: class Node{ int data; Node next; Node(int data){ this.data=data; ...

  6. [转]SQL Server 连接串关键字别名

    转自:http://stackoverflow.com/questions/3077412/what-is-the-difference-between-trusted-connection-and- ...

  7. LeetCode 168. Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...

  8. MySQL 教程分享

    MySQL 教程 MySQL 安装 MySQL 管理与配置 MySQL PHP 语法 MySQL 连接 MySQL 创建数据库 MySQL 删除数据库 MySQL 选择数据库 MySQL 数据类型 M ...

  9. PHP中对淘宝URL中ID提取

    <?php $taobao = 'taobao.com'; $tmall = 'tmall.com'; $guojitmall = 'tmall.hk'; $juhuasuan = 'ju.ta ...

  10. Metro Win8风格的按钮(Filp翻转)

    原地址->http://www.cnblogs.com/yk250/p/5661093.html 介绍:简约而不简单....颜色可随意调制,最好用Blend工具. 效果图如下:话说这个图会不会太 ...