react-string-replace is a lightweight JavaScript utility designed to safely replace substrings or regular expression matches within a given string or array, converting them into an array containing React components and strings. It is currently at stable version 2.0.1 and has seen active development, with recent tooling modernizations and breaking changes in major versions. The library's core differentiator is its ability to perform replacements suitable for React's rendering model without relying on `dangerouslySetInnerHTML`, thus maintaining React's built-in XSS protection. It offers a simple API for common tasks like highlighting text, converting URLs, or parsing mentions, and supports chaining for multiple replacement patterns. It has zero runtime dependencies and ships with TypeScript types, making it suitable for modern React applications.
npm install react-string-replaceVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to perform multiple chained string replacements on a single text string, transforming URLs, @-mentions, and hashtags into clickable React `<a>` elements, suitable for rendering within a React component.
Review and update any custom logic that relies on the `index` parameter within your replacement functions. Assume 0-based indexing for matches and adjust calculations accordingly if migrating from v1.x.
Ensure your regular expression pattern has at least one capturing group. For instance, to match 'foo', use `/(foo)/g`.
Organize your `reactStringReplace` calls sequentially, assigning the result of each call to a variable and using that variable as the input for the subsequent replacement. Refer to the quickstart example for a demonstration of chaining replacements.
Be aware that the function operates only on string segments when given an array input. If you need to process non-string elements, pre-process the array to convert them to strings or handle them separately before passing them to `reactStringReplace`.
This specific issue was largely addressed in `v1.1.0`. Ensure you are using `react-string-replace` `v1.1.0` or newer. If the problem persists, carefully review your regular expression for edge cases that might yield `undefined` in split results, or simplify the regex if possible to avoid ambiguous matches.
Upgrade to `react-string-replace` v2.0.0 or higher to utilize the more intuitive 0-based logical index. If upgrading is not an immediate option, you will need to adjust your logic to account for the older `index` numbering scheme (e.g., `(index - 1) / 2` to derive a 0-based logical index from the v1.x behavior).
No dependency data recorded yet.