querySelector, wildcard element match?

JavascriptDomDomparserSelectors Api

Javascript Problem Overview


Is there a way to do a wildcard element name match using querySelector or querySelectorAll?

The XML document I'm trying to parse is basically a flat list of properties

  • I need to find elements that have certain strings in their names.
  • I see support for wildcards in attribute queries but not for the elements themselves.

Any solution except going back to using the apparently deprecated XPath (IE9 dropped it) is acceptable.

Javascript Solutions


Solution 1 - Javascript

[id^='someId'] will match all ids starting with someId.

[id$='someId'] will match all ids ending with someId.

[id*='someId'] will match all ids containing someId.

If you're looking for the name attribute just substitute id with name.

If you're talking about the tag name of the element I don't believe there is a way using querySelector

Solution 2 - Javascript

I was messing/musing on one-liners involving querySelector() & ended up here, & have a possible answer to the OP question using tag names & querySelector(), with credits to @JaredMcAteer for answering MY question, aka have RegEx-like matches with querySelector() in vanilla Javascript

Hoping the following will be useful & fit the OP's needs or everyone else's:

// basically, of before:
var youtubeDiv = document.querySelector('iframe[src="http://www.youtube.com/embed/Jk5lTqQzoKA"]')

// after     
var youtubeDiv = document.querySelector('iframe[src^="http://www.youtube.com"]');
// or even, for my needs
var youtubeDiv = document.querySelector('iframe[src*="youtube"]');

Then, we can, for example, get the src stuff, etc ...

console.log(youtubeDiv.src);
//> "http://www.youtube.com/embed/Jk5lTqQzoKA"
console.debug(youtubeDiv);
//> (...)

Solution 3 - Javascript

Set the tagName as an explicit attribute:

for(var i=0,els=document.querySelectorAll('*'); i<els.length;
          els[i].setAttribute('tagName',els[i++].tagName) );

I needed this myself, for an XML Document, with Nested Tags ending in _Sequence. See JaredMcAteer answer for more details.

document.querySelectorAll('[tagName$="_Sequence"]')

I didn't say it would be pretty :) PS: I would recommend to use tag_name over tagName, so you do not run into interferences when reading 'computer generated', implicit DOM attributes.

Solution 4 - Javascript

I just wrote this short script; seems to work.

/**
 * Find all the elements with a tagName that matches.
 * @param {RegExp} regEx  regular expression to match against tagName
 * @returns {Array}       elements in the DOM that match
 */
function getAllTagMatches(regEx) {
  return Array.prototype.slice.call(document.querySelectorAll('*')).filter(function (el) { 
    return el.tagName.match(regEx);
  });
}
getAllTagMatches(/^di/i); // Returns an array of all elements that begin with "di", eg "div"

Solution 5 - Javascript

i'm looking for regex + not + multiClass selector, and this is what I got.

Hope this help someone looking for same thing!

// contain abc class
"div[class*='abc']" 

// contain exact abc class
"div[class~='abc']" 

// contain exact abc & def(case-insensitively)
"div[class~='abc'][class*='DeF'i]" 

// contain exact abc but not def(case-insensitively)
"div[class~='abc']:not([class*='DeF'i])" 

css selector doc: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

simple test: https://codepen.io/BIgiCrab/pen/BadjbZe

Solution 6 - Javascript

There is a way by saying what is is not. Just make the not something it never will be. A good css selector reference: https://www.w3schools.com/cssref/css_selectors.asp which shows the :not selector as follows:

:not(selector)	:not(p)	Selects every element that is not a <p> element

Here is an example: a div followed by something (anything but a z tag)

div > :not(z){
 border:1px solid pink;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErik AnderssonView Question on Stackoverflow
Solution 1 - JavascriptJaredMcAteerView Answer on Stackoverflow
Solution 2 - JavascriptStephaneAGView Answer on Stackoverflow
Solution 3 - JavascriptLorenz Lo SauerView Answer on Stackoverflow
Solution 4 - JavascriptcruzanmoView Answer on Stackoverflow
Solution 5 - JavascriptbigiCrabView Answer on Stackoverflow
Solution 6 - JavascriptSteve LloydView Answer on Stackoverflow