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. React Native 网络请求

    如下面的Code,分别介绍了GET,POST,以及使用XMLHttpRequest的Get请求. import React, { Component } from 'react'; import { ...

  2. Java Web 编程

    Java Web 编程 21天学通JAVA-WEB开发:http://files.cnblogs.com/files/maven-chao/Java_Web.zip

  3. lnmp平台菜鸟入门级笔记

                  LNMP平台搭建 Mysql安装  MySQL安装 回复收藏  分享    1 下载MySQL数据库l到/usr/local/src/[root@xin tmp]# cd ...

  4. C#调试心经续(转)

    断点篇 命中次数(Hit Counts) 右击断点,可以设置Hit Counts(命中次数),会弹出如下的对话框 当条件满足的时候断点会被命中(即即将被执行),这个命中次数是断点被命中的次数.默认是始 ...

  5. TObject、Pointer、Interface的转换

    unit Unit4; ));   ));   ));   //将Obj转为接口   //LInf1 := ITest(Pointer(LObj1));       //无法转换了,丢失了接口信息   ...

  6. win10 下 gulp-sass 无法使用的解决

    参考链接: http://stackoverflow.com/questions/28409100/try-reinstalling-node-sass-on-node-0-12 解决方法: npm ...

  7. Thread.join简单介绍

    百度了一下,终于明白了.这个解释最简单: 前提:join()方法肯定是被某个线程调用的.   A线程正在执行,突然执行的时候碰到了B.join(), 那么,A线程就必须要等到B线程执行完之后才能执行. ...

  8. C++坑点集合 - 1 隐式调用和默认实现的构造函数的坑

    C++是一个编译器会替你在背后做很多事情的语言,包括模板实例化,按需要创造隐式的构造函数,默认构造你没有显式构造的成员,按需进行隐式转换和饮食构造等等,如果没有彻底了解清楚,就容易被这些编译器背后做好 ...

  9. javascript网址收集

    1.模块的写法http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 2.模块规范 AMDhttp://www.ruanyifeng ...

  10. php中urldecode()和urlencode()

    urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%.urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其原 ...