我如何使用jQuery的.find方法和正则表达式作为参数? [英] How can i use jQuery's .find method with a regular expression as the parameter?
问题描述
我有一个xml,我想搜索相似的结果,类似于SQL中的 LIKE
。
我想做像 $(xml).find('regexHere')
这样的事情,但我不知道该怎么做。
如果这不是最好的方法,你能推荐别的吗?我需要搜索的数据在节点的参数范围内。
I have an xml that i'd like to search for "alike" results, similar to what LIKE
does in SQL.
I thought about doing something like $(xml).find('regexHere')
, but i don't know how to do that.
If that's not the best method, could you recommend something else? The data i need to search is within node's parameters.
编辑
$( xml).find('[cityName = Rio de Janeiro]')
给了我正在搜索的节点,因为它是完全相同的字符串。但是,如果我搜索 Rio
我没有得到任何结果,即使 Rio de Janeiro和 Rio 的节点 Claro有我正在寻找的字符串。
$(xml).find('[cityName=Rio de Janeiro]')
gives me exactly the node i'm searching for, because it's exactly the same string. But, if i search for Rio
i don't get any results, even though the nodes of Rio de Janeiro and Rio Claro have the string i'm looking for.
我尝试使用 $(xml).find('[cityName | =Rio ]')
没有运气,没有一个结果出现,但如果我改为里约热内卢
我得到了我所需要的。
I tried using $(xml).find('[cityName|="Rio"]')
without luck, not a single result comes up, but if i change to Rio de Janeiro
i get exactly what i need.
推荐答案
要查找具有与某个正则表达式匹配的属性的所有元素,可以使用 .find( *)
选择所有元素,然后 .filter(...)
删除任何不匹配的元素:
To find all elements that have an attribute matching a certain regex, you can use .find("*")
to select all elements and then .filter(...)
to remove any that don't match:
例如,要查找消息
属性包含hello world的所有节点:
For example, to find all nodes whose message
attribute contains "hello world":
var elements = $(xml).find("*").filter(function() {
return /[Hh]ello [Ww]orld/.test($(this).attr("message"));
});
这篇关于我如何使用jQuery的.find方法和正则表达式作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!