queryselectorAll()与正则表达式属性选择器 [英] queryselectorAll() with regex attribute selector

查看:223
本文介绍了queryselectorAll()与正则表达式属性选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var arr = [].slice.call(document.querySelectorAll("a[href*='pricing']"));

返回长度为6的数组。

var arr = [].slice.call(document.querySelectorAll("a[href*='tarification']"));

还生成一个长度为6的数组。

Also produces an array of length 6.

上下文是一个包含英文或法文页面的网站。两个版本中的任何一个或arr将在给定页面上返回6个结果,而另一个将生成一个空数组。

The context is a website with either English or French pages. Either of the two versions or arr will return 6 results on a given page while the other will produce an empty array.

我想动态地解释这个。因此,无论用户是在法语页面还是英语页面,我都知道其中一个版本将返回6个元素。我可以写一个 if()语句。但是有更整洁,更短的方式吗?我尝试了以下方法:

I would like to dynamically account for this. So regardless if the user is on a French or English page I know that one or the other versions will return 6 elements. I could write an if() statement. But is there a neater, shorter way? I tried the following:

var arr = [].slice.call(document.querySelectorAll("a[href*='(tarification|pricing)']"));

但是这也返回了一个空数组。

But that also returned an empty array.

推荐答案

这是关于css选择器,而不是正则表达式:

It's about css selectors, not regular expressions:

var arr = [].slice.call(document.querySelectorAll("a[href*='tarification'], a[href*='pricing']"));

以下选择定价目标的所有链接在他们的 href

a[href*='tarification'], a[href*='pricing']

这篇关于queryselectorAll()与正则表达式属性选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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