npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dom-regex

v0.0.3

Published

JavaScript library for querying DOM elements with Regular Expressions

Downloads

29

Readme

dom-regex

A JavaScript library for querying DOM elements with Regular Expressions.

This library is UMD wrapped so it can be used with or without a module loader such as requireJS.

Install

npm install --save dom-regex

Note: If this library is exposed directly to the window, it operates under the global variable DomRegex. Keep in mind you may be in an environment (ex: webpack) that requires you explicitly expose it to the window if you intend to use if from a window perspective.

Example Usage

let DomRegex = require('dom-regex');

// find all elements with any data- attribute
let elements = DomRegex.all(/data-[a-z]/);

The following examples and methods behave like querySelectorAll and querySelector. The methods that use all return all matching instances. The methods that use one return only the first instance found.

Querying through all the elements on the window

Methods:
DomRegex.all(regex [, attributeName])
DomRegex.one(regex [, attributeName])

When not using an attribute name, the regex is applied to the entire inside contents of the element's opening tag. If the tag is <div class="bar" data-id="141pop"> then the regex would be applied to div class="bar" data-id="141pop"

Finding all matches on the window:

let elements = DomRegex.all(/data-[a-z]/);

Finding the first match on the window:

let element = DomRegex.one(/data-[a-z]/);

Find all elements that have an attribute value that matches a Regular Expression

When using an attribute name, the regex is applied to the value of the attribute. If the element's opening tag was <div class="bar" data-id="141pop"> and the attribute name supplied was data-id, then the regex would be tested against 141pop.

Finding all matches against an attribute name:

// find all elements that have a data-id attribute that starts with 3 digits
let elements = DomRegex.all(/^\d{3}/, 'data-id');

Finding one match against an attribute name:

let element = DomRegex.one(/^\d{3}/, 'data-id');

Querying children of elements

Querying "inside" of elements is much like using querySelectorAll on an HTMLElement (element.querySelectorAll('...')). This method takes it a step further by offering the ability to query inside lists of elements, or by using selectors.

Methods:
DomRegex.all.inside(query, regex [, attributeName])
DomRegex.one.inside(query, regex [, attributeName])

Querying inside of a specific element:

// find all custom elements inside of #element
let element = document.getElementById('#element');
let customElement = DomRegex.all.inside(element, /^[a-z]+-[a-z]+/);

Querying inside elements using a selector:

// find all custom elements inside of any div with a class of "bar"
let customElement = DomRegex.all.inside('div.bar', /^[a-z]+-[a-z]+/);

Querying inside each element in a NodeList

let divs = document.querySelectorAll('div');
let elements = DomRegex.all.inside(divs, /\d+/, 'data-id');

Querying inside each element in an Array

let elements = DomRegex.all.inside(arrayOfElements, /\d+/, 'data-id');

Querying against elements

When querying against elements, you apply the regex directly to the element. This allows you to filter elements that you have already obtained.

Methods:
DomRegex.all.against(query, regex, [, attributeName])
DomRegex.one.against(query, regex, [, attributeName])

Applying regex to a specific element

let element = document.getElementById('#element');
let customElement = DomRegex.all.against(element, /^[a-z]+-[a-z]+/);
// if this passes, it would return the element in an array because we are using `all`

Applying regex to elements that match a selector

// this will test all divs with a class of "bar"
let customElement = DomRegex.all.against('div.bar', /\d{3}/, 'data-id');

Applying regex to elements inside of a NodeList

let divs = document.querySelectorAll('div');
let elements = DomRegex.all.against(divs, /\d+/, 'data-id');

Applying regex to elements inside of an array

let elements = DomRegex.all.against(arrayOfElements, /\d+/, 'data-id');

Other Examples:

Get all elements with a data- attribute

let elements = DomRegex.all(/data-[a-z]/);

Get all elements that have a data-id attribute that conforms to a pattern, (for example 3 numbers followed 3 letters):

let elements = DomRegex.all(/^\d{3}[a-z]{3}$/i, 'data-id');