{"id":15757782,"url":"https://github.com/rpearce/flexible-string-replace","last_synced_at":"2025-05-07T22:28:53.941Z","repository":{"id":34260213,"uuid":"173644933","full_name":"rpearce/flexible-string-replace","owner":"rpearce","description":"🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React. Replaces https://github.com/rpearce/highlightify and descendent of ideas for trying to fix https://github.com/iansinnott/react-string-replace","archived":false,"fork":false,"pushed_at":"2023-01-07T03:31:51.000Z","size":1179,"stargazers_count":1,"open_issues_count":16,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-07T21:14:14.872Z","etag":null,"topics":["react","replace-string","replace-text","string-interpolation","string-manipulation","string-replace","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rpearce.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-04T00:00:07.000Z","updated_at":"2023-03-04T18:03:32.000Z","dependencies_parsed_at":"2023-01-15T05:44:47.826Z","dependency_job_id":null,"html_url":"https://github.com/rpearce/flexible-string-replace","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpearce%2Fflexible-string-replace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpearce%2Fflexible-string-replace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpearce%2Fflexible-string-replace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpearce%2Fflexible-string-replace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpearce","download_url":"https://codeload.github.com/rpearce/flexible-string-replace/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252964320,"owners_count":21832665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["react","replace-string","replace-text","string-interpolation","string-manipulation","string-replace","typescript"],"created_at":"2024-10-04T09:40:25.961Z","updated_at":"2025-05-07T22:28:53.917Z","avatar_url":"https://github.com/rpearce.png","language":"JavaScript","readme":"# flexible-string-replace\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)\n[![npm version](https://img.shields.io/npm/v/@rpearce/flexible-string-replace.svg)](https://www.npmjs.com/package/@rpearce/flexible-string-replace) [![npm downloads](https://img.shields.io/npm/dm/@rpearce/flexible-string-replace.svg)](https://www.npmjs.com/package/@rpearce/flexible-string-replace) [![bundlephobia size](https://badgen.net/bundlephobia/minzip/@rpearce/flexible-string-replace)](bundlephobia.com/result?p=@rpearce/flexible-string-replace) [![Build Status](https://travis-ci.org/rpearce/flexible-string-replace.svg?branch=master)](https://travis-ci.org/rpearce/flexible-string-replace) [![Coverage Status](https://coveralls.io/repos/github/rpearce/flexible-string-replace/badge.svg?branch=master)](https://coveralls.io/github/rpearce/flexible-string-replace?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/ca092c1e9dfe10b455d7/maintainability)](https://codeclimate.com/github/rpearce/flexible-string-replace/maintainability)\n\n## Links\n* [Installation](#installation)\n* [Usage](#usage)\n* [All Contributors](#contributors)\n* [Authors](./AUTHORS)\n* [Changelog](./CHANGELOG.md)\n* [Contributing](./CONTRIBUTING.md)\n* [Code of Conduct](./CODE_OF_CONDUCT.md)\n\n## Installation\n```\n$ npm i @rpearce/flexible-string-replace\n```\nor\n```\n$ yarn add @rpearce/flexible-string-replace\n```\n\n## Usage\n`flexible-string-replace` mirrors the functionality of\n[`String.prototype.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)\nwith the following exceptions:\n* you can transform your match however you see fit\n* the return value is `[ * ]` – an Array of whatever type your `Replacement` is\n  or returns. For example, if you pass it a function, then you'll get back a\n  list of strings as well as your match transformations, whereas if you pass a\n  string, you'll get back simply a list of strings with your replacement applied\n* the argument order is `(Pattern, Replacement, String)` so that if you'd like\n  to [`curry`](https://ramdajs.com/docs/#curry) the function and partially apply\n  the first two arguments, you can then reuse those over and over again with\n  different strings\n\nNote: while these examples use some JSX, your matching function can return\nwhatever you like.\n\n```js\nimport flexibleStringReplace from '@rpearce/flexible-string-replace'\n\nconst str = 'The rain in Spain falls mainly on the plain. Spain is nice.'\nconst searchText = 'spain'\nconst replacement = (match, offset) =\u003e \u003cmark key={offset}\u003e{match}\u003c/mark\u003e\n\n\n// usage with RegExp pattern\nconst pattern = new RegExp(searchText, 'igm')\nflexibleStringReplace(pattern, replacement, str)\n// [\n//   'The rain in ',\n//   \u003cmark\u003eSpain\u003c/mark\u003e,\n//   ' falls mainly on the plain. ',\n//   \u003cmark\u003eSpain\u003c/mark\u003e,\n//   ' is nice.'\n// ]\n\n\n// usage with RegExp pattern and string Replacement\nconst pattern = 'Spain'\nflexibleStringReplace(pattern, 'foobar', str)\n// [\n//   'The rain in ',\n//   'foobar',\n//   ' falls mainly on the plain. ',\n//   'foobar',\n//   ' is nice.'\n// ]\n\n\n// usage with String pattern (no match)\nconst pattern = 'spain'\nflexibleStringReplace(pattern, replacement, str)\n// [\"The rain in Spain falls mainly on the plain. Spain is nice.\"]\n\n\n// usage with String pattern (match)\nconst pattern = 'Spain'\nflexibleStringReplace(pattern, replacement, str)\n// [\n//   'The rain in ',\n//   \u003cmark\u003eSpain\u003c/mark\u003e,\n//   ' falls mainly on the plain. Spain is nice.',\n// ]\n```\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://robertwpearce.com\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/592876?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eRobert Pearce\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/rpearce/flexible-string-replace/commits?author=rpearce\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#ideas-rpearce\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"https://github.com/rpearce/flexible-string-replace/commits?author=rpearce\" title=\"Tests\"\u003e⚠️\u003c/a\u003e \u003ca href=\"#example-rpearce\" title=\"Examples\"\u003e💡\u003c/a\u003e \u003ca href=\"https://github.com/rpearce/flexible-string-replace/commits?author=rpearce\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpearce%2Fflexible-string-replace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpearce%2Fflexible-string-replace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpearce%2Fflexible-string-replace/lists"}