jQuery选择器正则表达式

本文翻译自: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

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!"); 
  
Not matched
Not matched
Not matched
Not matched


#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
     });

你可能感兴趣的:(javascript,jquery,regex)