jQuery selector regular expressions

2018-12-31 02:55发布

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 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.

10条回答
浮光初槿花落
2楼-- · 2018-12-31 03:36

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

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

查看更多
闭嘴吧你
3楼-- · 2018-12-31 03:39

I'm just giving my real time example:

In native javascript I used following snippet to find the elements with ids starts with "select2-qownerName_select-result".

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

When we shifted from javascript to jQuery we've replaced above snippet with the following which involves less code changes without disturbing the logic.

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

查看更多
怪性笑人.
4楼-- · 2018-12-31 03:41

If your use of regular expression is limited to test if an attribut start with a certain string, you can use the ^ jquery selector

For example if your want to only select div with id starting with "abc", you can use $("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/

查看更多
高级女魔头
5楼-- · 2018-12-31 03:42
var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);

Here you go!

查看更多
登录 后发表回答