Javascript 不区分大小写的 xpath contains() 可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8474031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
case insensitive xpath contains() possible?
提问by Aron Woost
I'm running over all textnodes of my DOM and check if the nodeValue contains a certain string.
我正在运行我的 DOM 的所有文本节点并检查 nodeValue 是否包含某个字符串。
/html/body//text()[contains(.,'test')]
This is case sensitive. However, I also want to catch Test
, TEST
or TesT
. Is that possible with XPath (in JavaScript)?
这是区分大小写的。但是,我也想赶上Test
,TEST
或TesT
。XPath(在 JavaScript 中)可以实现吗?
回答by Tomalak
This is for XPath 1.0. If your environment supports XPath 2.0, see here.
这适用于 XPath 1.0。如果您的环境支持 XPath 2.0,请参见此处。
Yes. Possible, but not beautiful.
是的。可能,但并不美丽。
/html/body//text()[
contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
'test'
)
]
This would work for search strings where the alphabet is known beforehand. Add any accented characters you expect to see.
这适用于预先知道字母表的搜索字符串。添加您希望看到的任何重音字符。
If you can, mark the text that interests you with some other means, like enclosing it in a <span>
that has a certain class while building the HTML. Such things are much easier to locate with XPath than substrings in the element text.
如果可以,请使用其他方式标记您感兴趣的文本,例如<span>
在构建 HTML 时将其包含在具有特定类的 a 中。与元素文本中的子字符串相比,使用 XPath 定位这些东西要容易得多。
If that's not an option, you can let JavaScript (or any other host language that you are using to execute XPath) help you with building an dynamic XPath expression:
如果这不是一个选项,您可以让 JavaScript(或您用来执行 XPath 的任何其他宿主语言)帮助您构建动态 XPath 表达式:
function xpathPrepare(xpath, searchString) {
return xpath.replace("$u", searchString.toUpperCase())
.replace("$l", searchString.toLowerCase())
.replace("$s", searchString.toLowerCase());
}
xp = xpathPrepare("//text()[contains(translate(., '$u', '$l'), '$s')]", "Test");
// -> "//text()[contains(translate(., 'TEST', 'test'), 'test')]"
(Hat tip to @KirillPolishchuk's answer- of course you only need to translate those characters you're actually searchingfor.)
(对@KirillPolishchuk 的回答的提示- 当然,您只需要翻译您实际搜索的那些字符。)
This approach would work for any search string whatsoever, without requiring prior knowledge of the alphabet, which is a big plus.
这种方法适用于任何搜索字符串,无需事先了解字母表,这是一个很大的优势。
Both of the methods above fail when search strings can contain single quotes, in which case things get more complicated.
当搜索字符串可以包含单引号时,上述两种方法都会失败,在这种情况下事情会变得更加复杂。
回答by Kirill Polishchuk
More beautiful:
更美丽:
/html/body//text()[contains(translate(., 'TES', 'tes'), 'test')]
回答by kjhughes
XPath 2.0 Solutions
XPath 2.0 解决方案
Use lower-case():
/html/body//text()[contains(lower-case(.),'test')]
Use matches()regex matching with its case-insensitive flag:
/html/body//text()[matches(.,'test', 'i')]
回答by Andy
Yes. You can use translate
to convert the text you want to match to lower case as follows:
是的。您可以使用translate
将要匹配的文本转换为小写,如下所示:
/html/body//text()[contains(translate(.,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'),
'test')]
回答by Michael Kay
If you're using XPath 2.0 then you can specify a collation as the third argument to contains(). However, collation URIs are not standardized so the details depend on the product that you are using.
如果您使用的是 XPath 2.0,那么您可以将排序规则指定为 contains() 的第三个参数。但是,整理 URI 未标准化,因此详细信息取决于您使用的产品。
Note that the solutions given earlier using translate() all assume that you are only using the 26-letter English alphabet.
请注意,之前使用 translate() 给出的解决方案都假定您仅使用 26 个字母的英文字母表。
UPDATE:XPath 3.1 defines a standard collation URI for case-blind matching.
更新:XPath 3.1 定义了用于大小写匹配的标准排序规则 URI。
回答by Marvin Smit
The way i always did this was by using the "translate" function in XPath. I won't say its very pretty but it works correctly.
我总是这样做的方式是使用 XPath 中的“翻译”功能。我不会说它非常漂亮,但它工作正常。
/html/body//text()[contains(translate(.,'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLOMNOPQRSTUVWXYZ'),'TEST')]
hope this helps,
希望这可以帮助,