jQuery selector regular expressions

前端 未结 10 2286
傲寒
傲寒 2020-11-21 22:36

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector.

I have looked for this myself but have

相关标签:
10条回答
  • 2020-11-21 23:21

    If you just want to select elements that contain given string then you can use following selector:

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

    0 讨论(0)
  • 2020-11-21 23:29

    ids and classes are still attributes, so you can apply a regexp attribute filter to them if you select accordingly. Read more here: http://rosshawkins.net/archive/2011/10/14/jquery-wildcard-selectors-some-simple-examples.aspx

    0 讨论(0)
  • 2020-11-21 23:31
    var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);
    

    Here you go!

    0 讨论(0)
  • 2020-11-21 23:36

    These can be helpful.

    If you're finding by Contains then it'll be like this

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

    If you're finding by Starts With then it'll be like this

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

    If you're finding by Ends With then it'll be like this

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

    If you want to select elements which id is not a given string

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

    If you want to select elements which name contains a given word, delimited by spaces

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

    If you want to select elements which 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
         });
    
    0 讨论(0)
提交回复
热议问题