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 🙏

© 2025 – Pkg Stats / Ryan Hefner

regexp-capture-interpolation

v0.1.1

Published

Interpolate a regular expression match into a replacement string

Downloads

10

Readme

regexp-capture-interpolation

Interpolate regular expression captures and match variables (i.e. $1, $&) into a replacement string.

Usage

var Interpolation = require('regexp-capture-interpolation');

Interpolation.interpolate(
  ['key\tvalue', 'key', 'value', 0, 'key\tvalue\nkey2\tvalue2\n'],
  '$1: $2,'
); // returns "key: value,"

Exports

require('regexp-capture-interpolation')

Returns an object which has three functions, with the keys interpolate, interpolateSlash, and interpolateDollar.

These three functions each accept two arguments: a match array and a replacement string.

The match array corresponds to the arguments passed into the replacement function in String.prototype.replace(), that is, it should have the following values:

  • Matched substring
  • First capture
  • Second capture
  • ...
  • The offset of the matched substring within the whole string being examined.
  • The whole string being examined

Thus, the array should have at least three arguments, as captures being optional, their number being determined by the regular expression pattern.

interpolateDollar

This function is intended to correspond exactly to JavaScript's own replacement behaviour, i.e., the following should have the same result:

originalString.replace( pattern, replacement );

originalString.replace( pattern, function () {
  return Interpolation.interpolateDollar(arguments, replacement);
} );

Thus, it perfoms the following interpolations:

$&  Matched text
$`  The portion of the string that precedes the matched substring. 
$'  The portion of the string that follows the matched substring.
$1  The contents of the first captured substring.
$2  The contents of the second captured substring.
    ...
$$  A literal dollar character

Note that $1, $2... $n. are only interpolated if the pattern has at least n captures.

interpolateSlash

This function uses backslash as an escape character, which is useful for situations where you are replacing from user input.

It perfoms the following interpolations, corresponding broadly to JavaScript's String Literal syntax:

\1      The contents of the first captured substring.
\2      The contents of the second captured substring.
        ...
\\      A literal backslash character.
\0      A literal NUL (0x00) character.
\n      A newline.
\r      A carriage return.
\t      A tab character.
\v      A vertical tab character.
\$      A literal dollar character (this works for any non-word character).
\xhh    The codepoint at 0xhh.
\uhhhh  The codepoint at 0xhh.
\u{h}, \u{hhhh}, \x{hhh}, etc.

Note that \1, \2... \n. are only interpolated if the pattern has at least n captures.

Note also that if writing the replacement in JavaScript source, your backslashes may need escaping, i.e. the following are equivalent:

Interpolation.interpolateSlash(match, "\\1");
Interpolation.interpolateDollar(match, "$1");

interpolate

This function interpolates both slash-escaped and dollar-escaped tokens.

This function is designed to be used in a user-facing environment where DWIM (do what I mean) is an important principle, i.e. the user might expect to use dollar or slash syntax, or even both.

Author & Contributors

Daniel Perrett [email protected]

Copyright & License

This package is available under a MIT License - see the included LICENSE file for more information.