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

regex-tools

v0.3.8

Published

Tools for managing long regular expressions.

Downloads

38

Readme

Regular Expression Tools Build Status

I use regular expressions to do a lot of things, and managing a long regular expression could be painful. So I write this simple tool to manage long and complex regular expressions such as regular expressions of lex parser.

Install

npm install regex-tools --save-dev

Create Options File

Create a test-regex.js file and write something like this.

exports.options = {
  name: 'test',
  operation: 'combine',
  target: 'target.js', // support *.ts file, too
  flags: 'g',
  regexes: [/</, [/($term:\w+)/, /\d*/], />/],
};

Source File

Create a target.js and mark related code with /* /$test/ */ ($ followed by the name you configured in options file).

let testRegex = /* /$test/ */ /./;

let groups = testRegex.exec('<abc123>');

/* /$test/ */
let text = groups[0];

'<def456>'.replace(testRegex, function(/* /$test/ */ text) {
  return text;
});

A Basic Task

Create a task and run it.

let Gulp = require('gulp');
let RegexTools = require('regex-tools');

let glob = require('glob');

Gulp.task('update-regex', function() {
  let optionsFiles = glob.sync('*-regex.js');

  optionsFiles.forEach(function(path) {
    RegexTools.process(path);
  });
});

After that, target.js should look like:

let testRegex = /* /$test/ */ /<(\w+)\d*>/g;

let groups = testRegex.exec('<abc123>');

/* /$test/ */
let text = groups[0];
let term = groups[1];

'<def456>'.replace(testRegex, function(/* /$test/ */ text, term) {
  return text;
});

Problem solved! You may checkout demo for relatively more complex examples.

API References

An options file is a node module that exports options as default. The exported default value could be either RegexToolsOptions or RegexToolsOptions[].

And here's related type declarations:

interface RegexToolsOptions {
  /** name that will match related tag in target source file. */
  name: string;
  /** target source file. */
  target: string;
  /** only "combine" so far. */
  operation?: string;
  flags?: string;
  regexes: NestedRegexes;
}

type Lookahead = boolean | '=' | '!';

interface NestedRegexOptions {
  /** captured group name. */
  name?: string;
  /** whether to use `|`, default to false. */
  or?: boolean;
  /** whether to capture, default to false if `name` is not provided, otherwise true. */
  capture?: boolean;
  /** lookahead, `true` or `"="` for positive and `"!"` for negative. */
  lookahead?: Lookahead;
  /** ?, *, +, *?, +?, {1}, {1,}, {1,2} */
  repeat?: string;
  regexes: RegExp | NestedRegexArray | NestedRegexOptions;
}

interface NestedRegexArray
  extends Array<RegExp | NestedRegexArray | NestedRegexOptions> {}

type NestedRegexes = NestedRegexArray | NestedRegexOptions;

Back Reference Tracking

If you are using back reference, it will keep the index updated. That means you should write back references relative to the current part of regular expression.

The options below will result in /(distraction)(["'])\2/.

exports.options = {
  name: 'test',
  operation: 'combine',
  target: 'target.js',
  regexes: [/(distraction)/, /(["'])\1/],
};

However as \[number] can also be a char with code in octal form, it's kind of complex to deal with. Please avoid writing a char (instead of a back reference) in this form.

Named References

Another way to deal with back references is to use named references provided by the tool.

exports.options = {
  name: 'test',
  operation: 'combine',
  target: 'target.js',
  regexes: [/($quote:["'])/, /.*?/, /($quote)/],
};

When generating group/parameter/enumerator list, the name of named reference (as well as name specified in NestedRegexOptions) will be exported according to its capture index.

For groups and enumerator list, if you don't want some of them to show up, you may add a ~ between $ and name. Such as ($~quote:["']).

Tips

When updating group array aliases, the index starts at either 0 or 1, depending on your code.

You may also use require('regex-tools').combine() for more flexible usage, please check out source code (as it's typed) for more information.

License

MIT License.