§ Quick Take

import { strict as assert } from "assert";
import extract from "string-extract-class-names";

// extracts classes
assert.deepEqual(
  extract("div.first-class.second-class"),
  [".first-class", ".second-class"]
);

// and id's
assert.deepEqual(
  extract("div#brambles.gramples#croodles"),
  ["#brambles", ".gramples", "#croodles"]
);

// optionally, you can request ranges (see codsen.com/ranges/):
assert.deepEqual(
  extract("div.first-class.second-class", true),
  [
    [3, 15],
    [15, 28],
  ]
);

§ Examples

§ API

extract(
  inputString,
  [returnRangesInstead]
)

In other words, it's a function which takes two input arguments. Brackets mean that argument is optional.

§ API - Input

Input argumentTypeObligatory?Description
inputStringStringyesString to process
returnRangesInsteadBooleannoDefault - false - return arrays of strings - selectors; optionally - true - return array of ranges

By ranges we mean string slice ranges, arrays of two elements where both arguments match the String.slice first two arguments, beginIndex and endIndex.

§ The Use

We use string-extract-class-names to extract all the CSS class and id names from HTML/CSS in the library email-comb which detects and deletes the unused CSS styles.

§ Changelog

See it in the monorepo opens in a new tab, on Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detergent 6.1.1
Extracts, cleans and encodes text
📦 string-split-by-whitespace 1.7.0
Split string into array by chunks of whitespace
📦 string-convert-indexes 3.0.1
Convert between native JS string character indexes and grapheme-count-based indexes
📦 string-strip-html 7.0.3
Strips HTML tags from strings. No parser, accepts mixed sources
📦 string-remove-widows 1.7.3
Helps to prevent widow words in a text
📦 string-collapse-white-space 8.0.5
Replace chunks of whitespace with a single spaces
📦 string-remove-duplicate-heads-tails 4.0.2
Detect and (recursively) remove head and tail wrappings around the input string