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
Extract class (or id) name from a string
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, classes and id's don't come bare, just like that — there are other selectors and tags around. For example,
From this:
sometagname a.class-name:hoverwe want to extract this:
.class-nameOR, from this:
a.class-name[target=_blank]this:
.class-nameThis library will chop off heads — everything up to . (default) or # (set second input var) and tails — any of the following characters:
~ !@$%^&*()+=,./';:"?><[]\{}|`#and everything that follows after them (the space above is deliberately there).
Install
$ npm install --save string-extract-class-namesUse
var extract = require('string-extract-class-names')Test
$ npm testUses AVA.
API
extract(
string, // String. Input.
chopUpToNotIncluding // String. `.` for classes or `#` for id's. Default is `.`.
)
// => Extracted stringExamples
// 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-nameWe can extract id's be providing id selector as a second input parameter:
extract('a#id-name[href^="https"]', '#')
// => #id-nameContributing & testing
All contributions welcome. This library uses Standard JavaScript notation. See test.js. It's very minimalistic testing setup using AVA.
npm testIf you see anything incorrect whatsoever, raise an issue. PR's are welcome — fork, hack and PR.
Licence
MIT © Roy Reveltas