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

wildcard-regex

v3.0.2

Published

Super simple wildcard tools for generating string patterns and RegExp objects

Downloads

6,749

Readme

wildcard-regex npm Build Status

Super simple wildcard tools for generating string patterns and RegExp objects

Installation

To install wildcard-regex to your project, run:

npm install --save wildcard-regex

Getting Started

wildcard-regex is a simple set of tools that can convert raw wildcard patterns (strings or array of strings with wildcards denoted as *) to pattern strings or RegExp objects.

Using wildcard-regex to create a RegExp object for pattern matching is as simple as requiring it as a variable in your script and giving it a raw wildcard pattern.

ES6/ES2015 Syntax

import { wildcardRegExp } from 'wildcard-regex';
const regex = wildcardRegExp('Begin the sentence*and end it here');
// This generates the following RegExp object:
// /Begin the sentence.*and end it here/
regex.test('Begin the sentence, then add this portion and end it here');
// true

Older ES5 Syntax

var wildcard = require('wildcard-regex');
var regex = wildcard.wildcardRegExp('Begin the sentence*and end it here');
// This generates the following RegExp object:
// /Begin the sentence.*and end it here/
regex.test('Begin the sentence, then add this portion and end it here');
// true

You can also test for an array of wildcard patterns by passing them into wildcard-regex as an Array of Strings, as such:

ES6/ES2015 Syntax

import { wildcardRegExp } from 'wildcard-regex';
const regex = wildcardRegExp(['Test*This'], ['Or*This']);

Older ES5 Syntax

var wildcard= require('wildcard-regex');
var regex = wildcard.wildcardRegExp(['Test*This'], ['Or*This']);

If you want to store your wildcard pattern in a manner that does not work with RegExp objects, you might want to create a pattern string to represent the regex pattern instead of a full RegExp object.

This is easy to do by using the wildcardPattern method, which returns a string pattern instead of a RegExp object. Here's an example:

ES6/ES2015 Syntax

import { wildcardPattern } from 'wildcard-regex';
const pattern = wildcardPattern('*kevinzwhuang/wildcard-regex')
// This creates a string ready for storage and for conversion to a RegExp
// object later.
// '^.*kevinzwhuang/wildcard-regex$'
const regex = new RegExp(pattern);
// Call the RegExp constructor with the pattern to create a new RegExp object.

Older ES5 Syntax

var wildcard= require('wildcard-regex');
var pattern = wildcard.wildcardPattern('*kevinzwhuang/wildcard-regex')
// This creates a string ready for storage and for conversion to a RegExp
// object later.
// '^.*kevinzwhuang/wildcard-regex$'
var regex = new RegExp(pattern);
// Call the RegExp constructor with the pattern to create a new RegExp object.

API

wildcardRegExp(stringOrArray: String || Array) : Function => RegExp

Takes in a string or array of possible wildcard patterns as an argument.

Returns a RegExp object.

wildcardPattern(stringOrArray: String || Array) : Function => String

Takes in a string or array of possible wildcard patterns as an argument.

Returns a String that represents the regex pattern for easy storage and later conversion to a RegExp object..

Development

wildcard-regex is written to be portable for most Node and browser environments(even IE) without any need for transpilers. To develop your wildcard-regex, start by cloning this repo by running:

git clone https://github.com/kevinzwhuang/wildcard-regex.git

Then make sure to run npm install to install the devDependencies necessary for testing. To test wildcard-regex, run npm test.

Tests

wildcard-regex uses Mocha to test with Node for backend environments and Karma with Mocha to test for browser environments. You can run tests for both backend and frontend environments by running npm test.

To run tests for specific environments, you can run npm run test:node or npm run test:browser.

You can find tests within the ./tests/ folder.