This utility, `heti-findandreplacedomtext`, is a fork of the original `padolsey/findAndReplaceDOMText` library. Its core function is to search for regular expression matches within a given DOM node and subsequently replace or wrap each match with a specified node or piece of text. A key differentiator is its robust handling of matches that span across multiple DOM nodes, ensuring that all fragmented portions of a match are correctly processed and modified. The `heti-` fork specifically introduced an `offset` option to address and fix issues related to browser support for regex lookbehind, enhancing cross-browser compatibility. The package's current stable version, 0.5.0, was published over six years ago, indicating that active development has ceased and the package is largely unmaintained. It is primarily used for client-side DOM manipulation tasks without notable external runtime dependencies. For TypeScript users, community-maintained type definitions (`@types/findandreplacedomtext`) are available.
npm install heti-findandreplacedomtextVerified import paths — ran on the pinned version, not inferred.
Demonstrates wrapping text with `em` and `strong` tags, including across node boundaries, and replacing text with a custom element and uppercase transformation.
Change `/pattern/` to `/pattern/g` for global search.
Set `portionMode: 'first'` in the options object if you desire a different behavior for node boundary handling during replacement.
Sanitize any dynamic or user-provided input before using it within the `replace` function to create new DOM nodes or text content.
Optimize by providing a `filterElements` function to ignore irrelevant parts of the DOM, or use `preset: 'prose'` to automatically exclude non-textual elements and limit contexts, improving performance on typical prose content.
Install the type definitions: `npm install --save-dev @types/findandreplacedomtext`.
If using npm/bundlers, ensure `import findAndReplaceDOMText from 'heti-findandreplacedomtext';` is at the top of your file. If using a script tag, verify the `<script>` tag pointing to `findAndReplaceDOMText.js` is correctly placed and loaded before its usage. Also verify the package name in `npm install` and `import` matches `heti-findandreplacedomtext`.
Modify your `find` regular expression to include the global flag, e.g., change `/myword/` to `/myword/g`.
Review your `replace` function to ensure it consistently returns valid DOM nodes or strings. Consider adjusting the `portionMode` option (default is 'retain'). If you need to revert changes, save a clone of the original DOM subtree before modification, as the built-in `revert()` method may fail if the DOM is significantly altered elsewhere.
No dependency data recorded yet.