首页 文章

jQuery选择器正则表达式

提问于
浏览
528

我正在阅读使用jQuery选择器使用通配符或正则表达式(不确切的术语)的文档 .

我自己一直在寻找,但一直无法找到有关语法和如何使用它的信息 . 有谁知道语法的文档在哪里?

编辑:属性过滤器允许您根据属性值的模式进行选择 .

10 回答

  • 20
    $("input[name='option[colour]'] :checked ")
    
  • 6

    James Padolsey创建了一个允许正则表达式用于选择的wonderful filter .

    假设您有以下 div

    <div class="asdf">
    

    Padolsey的 :regex 过滤器可以这样选择它:

    $("div:regex(class, .*sd.*)")
    

    另外,请检查official documentation on selectors .

  • 231

    这些可能会有所帮助 .

    如果你在 Contains 找到它,那就是这样的

    $("input[id*='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    

    如果你在 Starts With 找到它,那就像是这样

    $("input[id^='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    

    如果你在 Ends With 找到它,那就是这样的

    $("input[id$='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    

    如果要选择 id is not a given string 的元素

    $("input[id!='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    

    如果要选择 id contains a given word, delimited by spaces 的元素

    $("input[id~='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    

    如果要选择 id is equal to a given string or starting with that string followed by a hyphen 的元素

    $("input[id|='DiscountType']").each(function (i, el) {
             //It'll be an array of elements
         });
    
  • 1

    添加一个jQuery函数,

    (function($){
        $.fn.regex = function(pattern, fn, fn_a){
            var fn = fn || $.fn.text;
            return this.filter(function() {
                return pattern.test(fn.apply($(this), fn_a));
            });
        };
    })(jQuery);
    

    然后,

    $('span').regex(/Sent/)
    

    将选择文本匹配/已发送/的所有span元素 .

    $('span').regex(/tooltip.year/, $.fn.attr, ['class'])
    

    将选择所有span元素,其类匹配/tooltip.year/ .

  • 6

    您可以使用filter函数应用更复杂的正则表达式匹配 . 这是一个与前三个div匹配的例子(现场演示here):

    <div id="abcd"></div>
    <div id="abccd"></div>
    <div id="abcccd"></div>
    <div id="abd"></div>
    
    $('div')
        .filter(function() {
            return this.id.match(/abc+d/);
        })
        .html("Matched!");
    
  • 0

    如果使用正则表达式仅限于测试attribut是否以某个字符串开头,则可以使用^ jquery选择器

    例如,如果您只想选择id为"abc"的div,则可以使用 $("div[id^='abc']")

    可以在这里找到许多非常有用的选择器以避免使用正则表达式:http://api.jquery.com/category/selectors/attribute-selectors/

  • 41

    我只是给出我的实时例子:

    在本机javascript中,我使用以下代码片段来查找带有id的元素,以“select2-qownerName_select-result”开头 .

    document.querySelectorAll("[id^='select2-qownerName_select-result']");

    当我们从javascript转移到jQuery时,我们用以下内容替换了上面的代码片段,这涉及更少的代码更改而不会干扰逻辑 .

    $("[id^='select2-qownerName_select-result']")

  • 313

    id和类仍然是属性,因此如果相应选择,可以对它们应用regexp属性过滤器 . 在这里阅读更多:http://rosshawkins.net/archive/2011/10/14/jquery-wildcard-selectors-some-simple-examples.aspx

  • 0

    如果您只想选择包含给定字符串的元素,则可以使用以下选择器:

    $(':contains("search string")')

  • 680
    var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);
    

    干得好!

相关问题