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-utilities

v2.3.0

Published

Tiny helpers for processing regex syntax

Downloads

913

Readme

regex-utilities

build status npm bundle size

Tiny utilities that the regex library makes available for reuse in its plugins. Useful for parsing and processing regular expression syntax in a lightweight way, when you don't need a full regex AST.

Constants

Context

Frozen object with the following properties for tracking regex syntax context:

  • DEFAULT - Base context.
  • CHAR_CLASS - Character class context.

Functions

For all of the following functions, argument expression is the target string, and needle is the regex pattern to search for.

  • Argument expression (the string being searched through) is assumed to be a flag-v-mode regex pattern string. In other words, nested character classes within it are supported when determining the context for a match.
  • Argument needle (the regex pattern being searched for) is provided as a string, and is applied with flags su.
  • If argument context is not provided, matches are allowed in all contexts. In other words, inside and outside of character classes.

replaceUnescaped

Arguments: expression, needle, replacement, [context]

Replaces all unescaped instances of a regex pattern in the given context, using a replacement string or function.

const str = '.\\.\\\\.[[\\.].].';
replaceUnescaped(str, '\\.', '@');
// → '@\\.\\\\@[[\\.]@]@'
replaceUnescaped(str, '\\.', '@', Context.DEFAULT);
// → '@\\.\\\\@[[\\.].]@'
replaceUnescaped(str, '\\.', '@', Context.CHAR_CLASS);
// → '.\\.\\\\.[[\\.]@].'

Details for the replacement argument:

  • If a string is provided, it's used literally without special handling for backreferences, etc.
  • If a function is provided, it receives two arguments:
    1. The match object (which includes groups, index, etc.).
    2. An object with extended details (context and negated) about where the match was found.

execUnescaped

Arguments: expression, needle, [pos = 0], [context]

Returns a match object for the first unescaped instance of a regex pattern in the given context, or null.

hasUnescaped

Arguments: expression, needle, [context]

Checks whether an unescaped instance of a regex pattern appears in the given context.

forEachUnescaped

Arguments: expression, needle, callback, [context]

Runs a function for each unescaped match of a regex pattern in the given context. The function receives two arguments:

  1. The match object (which includes groups, index, etc.).
  2. An object with extended details (context and negated) about where the match was found.

getGroupContents

Arguments: expression, contentsStartPos

Extracts the full contents of a group (subpattern) from the given expression, accounting for escaped characters, nested groups, and character classes. The group is identified by the position where its contents start (the string index just after the group's opening delimiter). Returns the rest of the string if the group is unclosed.