Giter Club home page Giter Club logo

path-to-regex's Introduction

Build Status

path-to-regex

  • Vanilla browser, AMD & CommonJS compatible
  • Works completely without hacks, polyfills etc
  • Extensive test suite with 800+ tests
  • No dependencies - one file
  • Fast and small (~1KB gzipped)

Usage

pathToRegex(<path = String|RegExp>[, options = Object]);

Turns given path into a regular expression

pathToRegex('/users/:userId/');
pathToRegex('/users/:userId/', {
  caseSensitive: true // default: false
});
  • path String to be converted into a regular expression.
    • or a regular expression to inherit custom method query
  • options
    • caseSensitive When true the matching will be case sensitive.

The return value of pathToRegex is a RegExp object with added properties (explanation down below)

pathToRegex.toFixed(<path = String);

Returns the fixed (static) portion of given path

pathToRegex.toFixed('/users/:userId/'); //-> /users/
pathToRegex.toFixed('/users/:userId');  //-> /users
pathToRegex.toFixed('/:page/');         //-> /
pathToRegex.toFixed('/i/like/turt*es'); //-> /i/like

var containsModifiers = pathToRegex.toFixed(path) === path;
  • options
    • path String of which to extract the fixed portion from

Modifiers

:capture

A mandatory capture group

var re = pathToRegex('/users/:userId');

re.query('/users/abc')        //-> ['abc']
re.query('/users/18')         //-> ['18']
re.query('/users/')           //-> null
re.query('/users/18/more')    //-> null

:optional?

An optional capture group

var re = pathToRegex('/users/:userId?');

re.query('/users/abc')      //-> ['abc']
re.query('/users/18')       //-> ['18']
re.query('/users/')         //-> [undefined]
re.query('/users/18/more')  //-> null

* (wildcard)

A zero-or-more modifier

var re = pathToRegex('/page/*/update*');

re.query('/page/index/updateStuff') //-> ['index', 'Stuff']
re.query('/page/updateStuff')       //-> [undefined, 'Stuff']
re.query('/page/update')            //-> [undefined, undefined]
re.query('/page/updaate')           //-> null

The returned RegExp

Is a proper RegExp instance but with to properties added:

  • Function query
    • Identical to the native RegExp.exec but it pops the first mandatory full-match
  • Number length The number of modifiers present in the path
    • pathToRegexp('/users/:userId?/:action).length => 2

path-to-regex's People

Contributors

unkelpehr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.