最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - queryselectorAll() with regex attribute selector - Stack Overflow

programmeradmin0浏览0评论
var arr = [].slice.call(document.querySelectorAll("a[href*='pricing']"));

Returns an array with length 6.

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

Also produces an array of length 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.

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.

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

Returns an array with length 6.

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

Also produces an array of length 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.

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.

Share Improve this question asked Nov 14, 2015 at 22:09 Doug FirDoug Fir 21.2k52 gold badges190 silver badges336 bronze badges 5
  • 1 a[href*='tarification'],a[href*='pricing'] – vp_arth Commented Nov 14, 2015 at 22:13
  • You want to know which one has 6 elements or how many elements has one of them (regardless which)? – Al.G. Commented Nov 14, 2015 at 22:13
  • @Al.G. I want to return an array of elements either based "tarification" or "pricing" in a oner. Looks like the first comment above might be what I'm after, will give it a try... – Doug Fir Commented Nov 14, 2015 at 22:14
  • I was just about to post an answer but thought about the other situation. Try it... – Al.G. Commented Nov 14, 2015 at 22:15
  • Anyway, I think you should to recognize(or even know, if it yours) localization and then search by relevant queries. – vp_arth Commented Nov 14, 2015 at 22:17
Add a comment  | 

1 Answer 1

Reset to default 17

It's about css selectors, not regular expressions:

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

The following selects all links with pricing or tarification in their href:

a[href*='tarification'], a[href*='pricing']
发布评论

评论列表(0)

  1. 暂无评论