JSPM

  • Created
  • Published
  • Downloads 13748
  • Score
    100M100P100Q140992F
  • License MIT

Extract class (or id) name from a string

Package Exports

  • string-extract-class-names

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-extract-class-names) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

string-extract-class-names

Standard JavaScript

Extract class (or id) name from a string

Build Status bitHound Overall Score Dependency Status devDependency Status Downloads/Month

Purpose

I was working on another project where I parsed some HTML and CSS and wanted to extract all the classes and id's. However, most of the time, the classes were wrapped in other selectors and tags.

From this:

sometagname a.class-name:hover

we want to extract this:

.class-name

OR, from this:

a.class-name[target=_blank]

extract this:

.class-name

This library will chop off everything up to . (default) or # (set second input var) and then — any of the following characters after the class/id name:

~ !@$%^&*()+=,./';:"?><[]\{}|`#

and everything that follows after them (the space above is deliberately there).

Install

$ npm install --save string-extract-class-names

Use

var extract = require('string-extract-class-names')

Test

$ npm test

Uses AVA.

API

extract(
  string,               // String. Input.
  chopUpToNotIncluding  // String. `.` for classes or `#` for id's. Default is `.`.
)
// => Extracted string

Examples

// second param is omitted, falls back to default "."
extract('div.class-name a[target=_blank]')
// => .class-name
// or with second parameter
extract('div.class-name a[target=_blank]', '.')
// => .class-name

We can extract id's be providing id selector as a second input parameter:

extract('a#id-name[href^="https"]', '#')
// => #id-name

Contributing & testing

All contributions welcome. This library uses Standard JavaScript notation. See test.js. It's very minimalistic testing setup using AVA.

npm test

If you see anything incorrect whatsoever, raise an issue. PR's are welcome — fork, hack and PR.

Licence

MIT © Roy Reveltas