jQuery 正则选择器

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!

时间: 2024-12-15 22:15:08

jQuery 正则选择器的相关文章

谜一样的jquery之$选择器

jquery是一个强大的js类库,提供了很多便利的操作方法并兼容不同的浏览器,一旦使用便欲罢不能,根本停不下来,今天我们就来解读一下这个神秘的jquery源代码. 前几天思考再三,自己尝试着封装了一下jquery的$选择器,然而并不完善,我只对id,class,和标签选择器进行了封装,发现其实如果实现浅层的封装那么我们很容易就能够实现,但是一旦我们尝试着选择器的层次嵌套就会出来很多大大小小的坑! 下面我们先来看一下我个人封装的jquery的选择器部分. window.$ = function (

jquery颜色选择器

本站下载 第二种:纯JAVASCRIPT: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2311"> <title>DW调色板</title> <script> var ColorHex=new Array('00','33','66','99','CC','FF') var SpColo

jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法

1 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 2 3 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 4 $("div") 选择所有的div标签元素,返回div元素数组 5 $(".myClass") 选择使用myClass类的css的所有元素 6 $("*") 选择文档中的所有的元素,可

关于jquery ID选择器的看法

最近看到一道前端面试题: 请优化selector写法:$(".foo div#bar:eq(0)") 我给出的答案会是: 1. $("#bar") 2.  $("div#bar") 下面说说我为什么会给出两种答案 1. 因为页面要求ID是唯一的,题目中的语句,可以直接优化成ID选择 2. 因为jQuery的选择器引擎用的是Sizzle,而$("div#bar")这样的写法,在Sizzle内部,会是一个从右往左的查找顺序,也就是

jQuery 复合选择器应用的几个例子

这篇文章主要介绍了jQuery 复合选择器应用的几个例子,本文例子所引用的jQuery版本为 jQuery-1.8.3.min.js,喜欢的朋友可以学习下 <!-- 本文例子所引用的jQuery版本为 jQuery-1.8.3.min.js --> 一. 复合选择器对checkbox的相关操作 ? 1 2 3 4 5 <input type="checkbox" id="ckb_1" />  <input type="chec

jQuery 过滤选择器

jQuery 选择器(一) 1.ID选择器根据控件ID获取jQeruy,相当于javascript的getelementById.使用方法:$("#myid"),获取ID等于myid的jquery对象. 2.标签选择器使用标签名称获取jQuery,相当于javascript的getElementsByTagName_r().使用方法:$("p"),获取所有p标签.3.class选择器class为元素的定义样式,根据class名称获取jquery对象.如:$(&quo

关于JQuery的选择器

jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div")           选择所有的div标签元素,返回div元素数组 $(".myClass")      选择使用myClass类的css的所有元素 $("*")        

jquery input 选择器

1.9.1 官方文档: 创建一个 <input> 元素必须同时设定 type 属性.因为微软规定 <input> 元素的 type 只能写一次. jQuery 代码: // 在 IE 中无效: $("<input>").attr("type", "checkbox"); // 在 IE 中有效: $("<input type='checkbox'>");   jquery inp

jQuery的选择器中的通配符[id^=&#39;code&#39;]

1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']");//id属性以code结束的所有input标签 $("input[id*='code']");//id属性包含code的所有input标签 (2)根据索引选择 $("tbody tr:even"); //选择索引为偶数的所有tr标签 $("tbody