
Product
Introducing Socket MCP for Claude Desktop
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
@rpearce/flexible-string-replace
Advanced tools
🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React
Archived and public domain now (LICENSE).
$ npm i @rpearce/flexible-string-replace
or
$ yarn add @rpearce/flexible-string-replace
flexible-string-replace
mirrors the functionality of
String.prototype.replace
with the following exceptions:
[ * ]
– an Array of whatever type your Replacement
is
or returns. For example, if you pass it a function, then you'll get back a
list of strings as well as your match transformations, whereas if you pass a
string, you'll get back simply a list of strings with your replacement applied(Pattern, Replacement, String)
so that if you'd like
to curry
the function and partially apply
the first two arguments, you can then reuse those over and over again with
different stringsNote: while these examples use some JSX, your matching function can return whatever you like.
import flexibleStringReplace from '@rpearce/flexible-string-replace'
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const searchText = 'spain'
const replacement = (match, offset) => <mark key={offset}>{match}</mark>
// usage with RegExp pattern
const pattern = new RegExp(searchText, 'igm')
flexibleStringReplace(pattern, replacement, str)
// [
// 'The rain in ',
// <mark>Spain</mark>,
// ' falls mainly on the plain. ',
// <mark>Spain</mark>,
// ' is nice.'
// ]
// usage with RegExp pattern and string Replacement
const pattern = 'Spain'
flexibleStringReplace(pattern, 'foobar', str)
// [
// 'The rain in ',
// 'foobar',
// ' falls mainly on the plain. ',
// 'foobar',
// ' is nice.'
// ]
// usage with String pattern (no match)
const pattern = 'spain'
flexibleStringReplace(pattern, replacement, str)
// ["The rain in Spain falls mainly on the plain. Spain is nice."]
// usage with String pattern (match)
const pattern = 'Spain'
flexibleStringReplace(pattern, replacement, str)
// [
// 'The rain in ',
// <mark>Spain</mark>,
// ' falls mainly on the plain. Spain is nice.',
// ]
[1.1.0] - 2025-07-26
FAQs
🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React
The npm package @rpearce/flexible-string-replace receives a total of 249 weekly downloads. As such, @rpearce/flexible-string-replace popularity was classified as not popular.
We found that @rpearce/flexible-string-replace demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.