Extract class (or id) name from a string

§ Quick Take

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

// extracts classes
  [".first-class", ".second-class"]

// and id's
  ["#brambles", ".gramples", "#croodles"]

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

§ Examples


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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detergent 5.11.7
Extracts, cleans and encodes text
📦 string-convert-indexes 2.0.2
Convert between native JS string character indexes and grapheme-count-based indexes
📦 string-remove-thousand-separators 3.0.72
Detects and removes thousand separators (dot/comma/quote/space) from string-type digits
📦 string-remove-widows 1.6.17
Helps to prevent widow words in a text
📦 string-fix-broken-named-entities 3.0.11
Finds and fixes common and not so common broken named HTML entities, returns ranges array of fixes
📦 string-collapse-leading-whitespace 3.0.2
Collapse the leading and trailing whitespace of a string
📦 string-find-malformed 1.1.16
Search for a malformed string. Think of Levenshtein distance but in search.