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

string-extractor

v0.0.1

Published

Regular expression sugar for getting data out of strings.

Downloads

33

Readme

string-extractor.js npm Version Build Status Coverage Status

Regular expression sugar for getting data out of strings.

Usage

var stringExtractor = require('string-extractor');

var pattern = '*/{{ year: 4d }}-{{ month: d }}-{{ slug }}.((txt|m*))';
var extract = stringExtractor(pattern);

extract('foo/2014-01-bar.txt');      //=> { year: '2014', month: '01', slug: 'bar' }
extract('foo/2014-01-bar.md');       //=> { year: '2014', month: '01', slug: 'bar' }
extract('foo/2014-01-bar.markdown'); //=> { year: '2014', month: '01', slug: 'bar' }
extract('foo'); //=> false
  1. A * represents a wildcard, which matches one or more characters. We can have consecutive wildcards. For example, *** represents a sequence of three or more characters.

  2. Enclose option groups in (( brackets )). An option group matches any one of the specified strings separated by a pipe |. Each string can contain wildcards. As in our example, ((m*|txt)) matches txt, md, and markdown.

  3. Enclose capturing groups in {{ curly braces }}. A capturing group comprises a name and an optional printf-like “formatter”. If specified, the formatter must be preceded by a colon :. Each formatter comprises a length and a “type”. A d type means one or more digits, while an s type means one or more characters (including digits). Some examples of valid formatters:

    • Type only — d, s
    • Length only — 4
    • Both — 4d, 4s
  4. If the given pattern does not contain any capturing groups, matching it with a str will return:

    • true if str matches the pattern, and
    • false otherwise.

    If the given pattern contains at least one capturing group, matching it with a str will return:

    • an object literal of values extracted from the str if str matches the pattern, and
    • false otherwise.
  5. Matching is case-sensitive. Set opts.ignoreCase to true to enable case-insensitive matching:

    var pattern = '{{ title }}.jpg';
    var opts = { ignoreCase: true };
    var extract = stringExtractor(pattern, opts);
    
    extract('foo.jpg'); //=> { title: 'foo' }
    extract('foo.JPG'); //=> { title: 'foo' }

Read the tests for more usage examples.

API

var stringExtractor = require('string-extractor');

var extract = stringExtractor(pattern [, opts])

Compiles the specified string pattern into a regular expression. opts is an object literal; set opts.ignoreCase to true to enable case-insensitive matching.

var results = extract(str)

extract is a function that uses the compiled regular expression to extract values from the specified str. It returns an object literal, with the extracted values keyed to the names of the capturing groups.

Installation

Install via npm:

$ npm i --save string-extractor

Changelog

  • 0.0.1
    • Initial release

License

MIT