我可以在querySelectorAll中使用正则表达式吗? [英] Can I use a regular expression in querySelectorAll?
问题描述
在我正在做的页面上,我将以自定义链接
这样的元素结束:
On a page I'm doing I will be ending up with custom link
elements like this:
<link rel="multiply" type="service/math" src="path/to/service">
<link rel="substract" type="service/math" src="path/to/service">
...
我正在尝试使用 querySelectorAll
检索指定类型 service /...
的所有链接元素,并且无处可去。
I'm trying to use querySelectorAll
to retrieve all link elements with a type service/...
specified and am getting nowhere.
目前我选择了这个:
root.querySelectorAll('link');
这给了我所有< link>
元素当我只想要类型为的服务/.*
which gives me all <link>
elements when I only want the ones with type service/.*
问题:
我可以在QSA选择器中添加正则表达式吗?如果是这样,怎么做?
Questions:
Can I add a regex to a QSA selector? If so, how to do it?
推荐答案
你不能在选择器中真正使用正则表达式,但CSS选择器是强大的足以满足您的需求,其灵感来自正则表达式的开头语法。
You can't really use a regular expression in a selector but CSS selectors are powerful enough for your need with a "starts with" syntax inspired by regexes.
您可以使用子字符串匹配属性选择器: link [type ^ =服务]
You can use a substring matching attribute selectors : link[type^=service]
使用属性<$ c $读取code>类型的节点 c>键入以service开头
Reads "Nodes of type link
with an attribute type
starting with "service"
来自正式规范:
[att ^ = val]
表示具有att属性的元素,其值以前缀val开头。如果val是空字符串,则选择器不代表任何内容。
Represents an element with the att attribute whose value begins with the prefix "val". If "val" is the empty string then the selector does not represent anything.
这篇关于我可以在querySelectorAll中使用正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!