jQuery选择器正则表达式

本文探讨了如何在jQuery选择器中使用正则表达式或通配符进行元素选择。文中提到了James Padolsey创建的过滤器,允许通过正则表达式进行选择,并提供了使用^符号匹配以特定字符串开头的属性值的方法。此外,还分享了多个实用的jQuery选择器,以避免直接使用正则表达式。
摘要由CSDN通过智能技术生成

本文翻译自:jQuery selector regular expressions

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector. 我正在阅读使用jQuery选择器使用通配符或正则表达式(不确定的确切术语)的文档。

I have looked for this myself but have been unable to find information on the syntax and how to use it. 我自己一直在寻找,但一直无法找到有关语法和如何使用它的信息。 Does anyone know where the documentation for the syntax is? 有谁知道语法的文档在哪里?

EDIT: The attribute filters allow you to select based on patterns of an attribute value. 编辑:属性过滤器允许您根据属性值的模式进行选择。


#1楼

参考:https://stackoom.com/question/nUb/jQuery选择器正则表达式


#2楼

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

#3楼

James Padolsey created a wonderful filter that allows regex to be used for selection. James Padolsey创建了一个精彩的过滤器 ,允许正则表达式用于选择。

Say you have the following div : 假设你有以下div

<div class="asdf">

Padolsey's :regex filter can select it like so: Padolsey :regex过滤器可以选择它:

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

Also, check the official documentation on selectors . 另外,查看选择器官方文档


#4楼

You can use the filter function to apply more complicated regex matching. 您可以使用filter函数来应用更复杂的正则表达式匹配。

Here's an example which would just match the first three divs: 这是一个与前三个div匹配的示例:

 $('div') .filter(function() { return this.id.match(/abc+d/); }) .html("Matched!"); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="abcd">Not matched</div> <div id="abccd">Not matched</div> <div id="abcccd">Not matched</div> <div id="abd">Not matched</div> 


#5楼

If your use of regular expression is limited to test if an attribut start with a certain string, you can use the ^ jquery selector 如果使用正则表达式仅限于测试attribut是否以某个字符串开头,则可以使用^ jquery选择器

For example if your want to only select div with id starting with "abc", you can use $("div[id^='abc']") 例如,如果您只想选择ID为“abc $("div[id^='abc']") ,则可以使用$("div[id^='abc']")

A lot of very useful selectors to avoid use of regex can be find here : http://api.jquery.com/category/selectors/attribute-selectors/ 可以在这里找到许多非常有用的选择器以避免使用正则表达式: http//api.jquery.com/category/selectors/attribute-selectors/


#6楼

These can be helpful. 这些可能会有所帮助。

If you're finding by Contains then it'll be like this 如果你是通过Contains找到它那么就是这样的

    $("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 如果您通过Starts With找到它,那么它就像这样

    $("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 如果你是通过Ends With找到它那么就像这样

     $("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 如果要选择id不是给定字符串的元素

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

If you want to select elements which id contains a given word, delimited by spaces 如果要选择id包含给定单词的元素,则用空格分隔

     $("input[id~='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 如果要选择id等于给定字符串或以该字符串后跟连字符开头的元素

     $("input[id|='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值