我可以在 querySelectorAll 中使用正则表达式吗? [英] Can I use a regular expression in querySelectorAll?

查看:41
本文介绍了我可以在 querySelectorAll 中使用正则表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在做的页面上,我最终会得到这样的自定义 link 元素:

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');

当我只想要类型为 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^=service]

You can use a substring matching attribute selectors : link[type^=service]

读取具有以service"开头的属性 typelink 类型的节点

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆