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

regexpx

v0.1.5

Published

Adds the flags 'x', 'U', 's' and 'n' to JavaScripts native Regular Expressions

Downloads

3

Readme

This module exports a single function which turns template strings into RegExp objects, with the following benefits:

New flags

It adds support for the additional regular expression flags n, s, U, x, and X known from other languages:

  • n/noCapture: Turns all unnamed capturing groups into non-capturing groups.
  • s/singleLine: Makes the . match newline characters.
  • U/ungreedy: Makes all greedy quantifiers ungreedy, and all ungreedy quantifiers greedy.
  • x/extended: Whitespaces and comments after # are ignored unless escaped. Is active by default and (currently) can't be turned off.
  • X/extra: Forbid unnecessary escapes. A \ followed by a character (sequence) without special meaning is a SyntaxError.

New syntax:

It adds support for the additional regular expression features:

  • Named capturing groups: ...
  • Forbid octal escapes in the input: Octal escapes are generally confusing and even more so in regular expressions: The \10 in /(.)(.)(.)(.)(.)(.)(.)(.)(.)(x)\10/ is a reference to the capturing group (x). The \10 in /(.)(.)(.)(.)(.)(.)(.)(.)(.)[x]\10/ it is the char with code 8. Therefore, and because octal escapes in template strings are (currently) SyntaxErrors themselves, thy are not allowed by RegExpX. To reference unnamed capturing groups, use $1 or \k<0>

Substitutions:

...

  • string: ...
  • RegExp: ...
  • array: ...
  • object: ...

API:

const RegExpX = require('regexpx') || window.RegExpX; // CommonJS
/**
 * Transforms a template string into a RegExp object. May be called in two different ways:
 *
 * - to specify flags / options:
 *     @param  {string|object}  options  Either a string of flag characters or an object of flags properties.
 *         Examples:
 *             'i' adds the native case insensitivity flag.
 *             'gimuy' adds all native flags.
 *             'g-im-uy' adds the flags 'g', 'm' and 'y', and removes the flags 'i' and 'u'.
 *             { global: true, } adds the native 'g' flag.
 *             { ungreedy: false, } removes the 'U' flag (if previously added).
 *             'o' would add 'o' as a (unrecognised) native flag (which probably throws in the RegExp constructor).
 *     @return {function}                The RegExpX function with the new options bound.
 *                                       Can now be used to create RexExp objects with these options or can have more options bound.
 *
 * - to create a RegExp object call as a template string tag function:
 *     Since it uses the template strings '.raw' property, no additional escaping compared to regular RegExp literal is required,
 *     except that whitespaces and '#' characters that are not to be removed need to be escaped.
 *     Note: The sequence '${' needs to be escaped to '\${' or '$\{' and will then appear as '\${' or '$\{' in the resulting RegExp.
 *     @param  {object}  hasRaw          An object with a .raw property which is an array of strings.
 *     @param  {...any}  substitutions   Additional values to substitute into the template string.
 *     @invariant                        substitutions.length must be exactly hasRaw.raw.length - 1.
 *     @return {RegExp}                  Enhanced RexExp object.
 */

Examples:

...