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

replace-dom-text

v1.0.0

Published

A lightweight utility to find and replace text within DOM nodes.

Readme

replace-dom-text

npm version npm bundle size TypeScript License: MIT ESM Only

A modern TypeScript + ESM rewrite of James Padolsey’s findAndReplaceDOMText

Search, replace, or wrap matching text in the DOM — even when matches span multiple nodes. Now written in TypeScript, distributed as ES modules, and fully typed for better developer experience.

✨ Features

  • 🔍 Find text using RegExp or string
  • 🧩 Replace or wrap matches with text or DOM elements
  • 💬 Supports cross-node matches
  • 🧠 Fully typed API with IntelliSense
  • ⚙️ Built-in preset: 'prose' for natural text replacement
  • ♻️ Supports revert() to undo replacements
  • 📦 Ships as pure ESM + .d.ts definitions

🚀 Installation

npm install replace-dom-text

Or via CDN:

<script type="module">
  import replaceDOMText from 'https://cdn.jsdelivr.net/npm/replace-dom-text/+esm'
</script>

🧩 Basic Usage

<p id="t">123 456 Hello</p>
import replaceDOMText from 'replace-dom-text'

replaceDOMText(document.getElementById('t')!, {
  find: /Hello/,
  wrap: 'em',
})

Result:

<p id="t">123 456 <em>Hello</em></p>

💡 Cross-Node Matching

<p id="t">123 456 Hell<span>o Goodbye</span></p>
replaceDOMText(document.getElementById('t')!, {
  find: /Hello/,
  wrap: 'em',
})

Result:

<p id="t">
  123 456 <em>Hell</em><span><em>o</em> Goodbye</span>
</p>

⚙️ API

replaceDOMText(
  element: Element | Text,
  options: Options,
): Finder

Options

| Option | Type | Description | | ------------------ | ---------------------------------------------- | ------------------------------------------------------------------------------------------------ | | find | RegExp \| string | Text or pattern to search for. Add /g for multiple matches. | | replace | string \| (portion, match) => string \| Node | Replace matches with text or a Node. Supports $0, $1, $&, $', $``. | | **wrap** | string | Node | Wrap each match in an element ('span', 'em', etc.) or clone a provided node. | | **wrapClass** | string | CSS class to apply to the wrapping element. Ignored ifwrapis not used. | | **portionMode** |'retain' | 'first' | Preserve node boundaries (retain, default) or merge into the first node (first). | | **filterElements** | (el: Element) => boolean | Returnfalseto skip specific elements. | | **forceContext** |boolean | (el: Element) => boolean | Force elements to act as their own matching contexts. | | **preset** |'prose' | Ignore non-text elements (, `, etc.) and restrict matches within block elements. |

🔤 Using a Function for replace

replaceDOMText(document.getElementById('container')!, {
  find: 'function',
  replace: (portion, match) => `[${portion.index}]`,
})

Input:

<div id="container">Explaining how to write a replace <em>fun</em>ction</div>

Output:

<div id="container">Explaining how to write a replace <em>[0]</em>[1]</div>

🎨 Wrapping Matches

replaceDOMText(document.getElementById('container')!, {
  find: 'with ',
  wrap: 'em',
  wrapClass: 'highlight',
})

CSS:

.highlight {
  background: yellow;
}

Result:

<em class="highlight">with </em>

🧱 Context Control

Prevent matches from crossing certain element boundaries:

replaceDOMText(document.getElementById('test')!, {
  find: 'amazing',
  wrap: 'em',
  forceContext: el => el.matches('p'),
})

🧰 Instance API

const finder = replaceDOMText(node, options)

// Revert changes
finder.revert()

⚠️ Reversion only works if the DOM has not been modified after replacement.

🧠 Presets

preset: 'prose'

Optimized for prose and text content. Skips non-prose elements and prevents matches across block boundaries.

replaceDOMText(element, {
  preset: 'prose',
  find: 'something',
  replace: 'something else',
})

🧾 Type Definitions

TypeScript users get full type support out of the box:

import replaceDOMText, {
  Finder,
  Portion,
  Options,
} from 'replace-dom-text'

🧩 Example Projects

📜 License

MIT License Originally by James Padolsey TypeScript & ESM rewrite © 2025 by Yufan Sheng