{"id":30261666,"url":"https://github.com/peerigon/batch-replace","last_synced_at":"2025-08-15T20:55:29.767Z","repository":{"id":14666525,"uuid":"17385199","full_name":"peerigon/batch-replace","owner":"peerigon","description":"Perform multiple str.replace() with one operation.","archived":false,"fork":false,"pushed_at":"2020-08-27T12:54:48.000Z","size":40,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-07-10T23:41:26.205Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peerigon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-04T00:31:20.000Z","updated_at":"2020-08-27T12:49:34.000Z","dependencies_parsed_at":"2022-09-05T11:20:35.074Z","dependency_job_id":null,"html_url":"https://github.com/peerigon/batch-replace","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/peerigon/batch-replace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Fbatch-replace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Fbatch-replace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Fbatch-replace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Fbatch-replace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peerigon","download_url":"https://codeload.github.com/peerigon/batch-replace/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Fbatch-replace/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270467617,"owners_count":24588805,"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","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-08-15T20:55:25.059Z","updated_at":"2025-08-15T20:55:29.762Z","avatar_url":"https://github.com/peerigon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# batch-replace\n\n**Perform multiple str.replace() with one operation.**\n\nIn some situations it's not possible to chain multiple calls of `replace()` because you don't want to pass the result of the first operation to the second:\n\n```javascript\n\"ab\".replace(/a/g, \"b\").replace(/b/g, \"c\"); // returns \"cc\" instead of \"bc\"\n```\n\nWith **batch-replace** it's possible to replace all patterns at once:\n\n```javascript\nvar replace = require(\"batch-replace\");\n\nreplace(/a/g).with(\"b\").and(/b/g).with(\"c\").in(\"ab\"); // returns bc\n```\n\nYou can even create \"replacement modules\" for common tasks and to improve readability:\n\n```javascript\nreplace.hyperlinks().emoticons().in(message);\n```\n\nThe chainable api creates a queue of replacement modules behind the scenes. If you need the same queue over and over again you can save a reference to the queue by calling `.queue()`:\n\n```javascript\nvar enhanceMessage = replace.hyperlinks().emoticons().queue();\n\nenhanceMessage(\"Check out example.com :)\");\n// returns 'Check out \u003ca href=\"http://example.com\" target=\"_blank\"\u003eexample.com\u003c/a\u003e \u003cimg srg=\"/img/smilies/grin.jpg\" /\u003e'\n\nenhanceMessage(\"Check out nodejs.org :)\");\n// returns 'Check out \u003ca href=\"http://nodejs.org\" target=\"_blank\"\u003enodejs.org\u003c/a\u003e \u003cimg srg=\"/img/smilies/grin.jpg\" /\u003e'\n```\n\n\u003cbr\u003e\n\n## Setup\n\n[![npm status](https://nodei.co/npm/batch-replace.svg?downloads=true\u0026stars=true)](https://npmjs.org/package/batch-replace)\n\n[![build status](https://travis-ci.org/peerigon/batch-replace.svg)](http://travis-ci.org/peerigon/batch-replace)\n[![dependencies](https://david-dm.org/peerigon/batch-replace.svg)](http://david-dm.org/peerigon/batch-replace)\n[![devDependency Status](https://david-dm.org/peerigon/batch-replace/dev-status.svg)](https://david-dm.org/peerigon/batch-replace#info=devDependencies)\n[![coverage status](https://img.shields.io/coveralls/peerigon/batch-replace.svg)](https://coveralls.io/r/peerigon/peerigon/batch-replace?branch=master)\n\n[![browser support](https://ci.testling.com/peerigon/batch-replace.png)\n](https://ci.testling.com/peerigon/batch-replace)\n\n\u003cbr\u003e\n\n## Conflicts\n\nIf multiple patterns match the exact same string, the latter will succeed:\n\n```javascript\nreplace(/./g).with(\" \").and(/b/g).with(\"B\").in(\"b\");\n// returns 'B'\n```\n\nIf multiple patterns match parts of the same string, the match with the lower index succeeds:\n\n```javascript\nreplace(/abc/g)\n  .with(\"ABC\") // index = 0\n  .and(/abc/g)\n  .with(\"Alphabet\") // index = 0\n  .and(/b/g)\n  .with(\"B\") // index = 1\n  .and(/c/g)\n  .with(\"C\") // index = 2\n  .in(\"abc\");\n// returns 'Alphabet'\n```\n\n\u003cbr\u003e\n\n## Replacement modules\n\nA replacement module is an object with a `pattern`- and a `replace`-property:\n\n```javascript\n{\n    pattern: /abc/g,\n    replace: \"ABC\"\n}\n```\n\nThe `replace`-property may also be a function which accepts the `match` returned by `pattern.exec()` and returns the new string:\n\n```javascript\n{\n    pattern: /abc/g,\n    replace: function (match) {\n        return match[0].toUpperCase();\n    }\n}\n```\n\nYou can add these modules by calling `module()`:\n\n```javascript\nreplace.module(\"abcToUppercase\", {\n  pattern: /abc/g,\n  replace: \"ABC\",\n});\n```\n\nAfter that you can chain them like this:\n\n```javascript\nreplace.abcToUppercase().in(\"abcdefgh\"); // returns \"ABCdefgh\"\n```\n\n**batch-replace** comes with useful modules which are completely optional to use (see below). Please feel free to open a pull request if you implemented another useful replacement module.\n\n### hyperlinks\n\nThis module wraps all url-like patterns in a text with `\u003ca\u003e`-tags:\n\n```javascript\nreplace.use(require(\"batch-replace/plugins/hyperlinks\"));\n\nreplace.hyperlinks().in(\"Hi, please take a look at example.com\");\n// returns 'Hi, please take a look at \u003ca href=\"http://example.com\" target=\"_blank\"\u003eexample.com\u003c/a\u003e'\n```\n\nIn case you need to modify the generated html just overwrite the `hyperlink`-function just like that:\n\n```javascript\nvar hyperlinks = require(\"batch-replace/plugins/hyperlinks\");\n\n// If the text was 'Hi, please take a look at example.com'\n// url will be 'http://example.com' and str will be 'example.com'\nhyperlinks.hyperlink = function (url, str) {\n  return '\u003ca href=\"' + url + '\"\u003e' + str + \"\u003c/a\u003e\";\n};\n```\n\n### html\n\nThis module escapes all special html characters like `\u0026` `\u003c` `\u003e` `\"` and `'` with their save entity counterparts. Use this module if you need to escape user data to prevent XSS attacks.\n\n```javascript\nreplace.use(require(\"batch-replace/plugins/html\"));\n\nreplace.html().in(\"This is a mean \u003cscript\u003ealert('XSS attack!')\u003c/script\u003e\");\n// returns 'This is a mean \u0026lt;script\u0026gt;alert(\u0026#x27;XSS attack!\u0026#x27;)\u0026lt;/script\u0026gt;'\n```\n\n\u003cbr\u003e\n\n## API\n\n### replace(pattern: RegExp)\n\nCreates a new queue where patterns and replacements can be registered. The given `pattern` is pushed into the new queue.\n\n### .with(replacement: String|Function)\n\nRegisters the `replacement` to the current pattern in the queue.\n\n### .and(pattern: RegExp)\n\nPushes a new `pattern` into the queue.\n\n### .in(str: String)\n\nRuns all replacement modules in the queue on the given string.\n\n### .queue(): Function\n\nReturns a standalone function that takes a string and runs the configured replacement modules on it. Use this function if you need the same queue over and over again.\n\n### .module(name: String, module: Object)\n\nPublishes the `module` under `replace[name]`. Write replacement modules for common replacement tasks and don't hesitate to create a pull-request so everyone benefits.\n\n\u003cbr\u003e\n\n## Compatibility\n\nIt is worth noting that the current api is not designed for ES3-environments (IE8 and Android 2.x) due usage of reserved keywords like `with` and `in`. If you need to support these environments and you don't want to use bracket notation (e.g. `[\"in\"]`), you can also use the \"ugly api\":\n\n### replace(str: String, modules: Array): String\n\nApplies all `modules` on the given string and returns the result. Example:\n\n```javascript\nreplace(\"abcd\", [\n  {\n    pattern: /a/g,\n    replace: \"b\",\n  },\n  {\n    pattern: /b/g,\n    replace: \"c\",\n  },\n]); // returns 'bccd'\n```\n\n**batch-replace** requires several polyfills for ES5-features, so you should be sure to include [es5-shims](https://github.com/es-shims/es5-shim).\n\n\u003cbr\u003e\n\n## License\n\nMIT\n\n## Sponsors\n\n[\u003cimg src=\"https://assets.peerigon.com/peerigon/logo/peerigon-logo-flat-spinat.png\" width=\"150\" /\u003e](https://peerigon.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerigon%2Fbatch-replace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeerigon%2Fbatch-replace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerigon%2Fbatch-replace/lists"}