Skip to content
m4bwav edited this page Sep 30, 2026 · 2 revisions

replace-string-at-position

replace-string-at-position has one function. You give it a text, the part being replaced, the new text and the index where the replaced part starts, and it returns the text with that one part swapped. It is for the case where you already know the position, for example from RegExp.prototype.exec or String.prototype.indexOf, and want to change that occurrence and no other. It does not search, it does not check that the replaced part is at the position (only its length counts), and it has no dependencies.

npm install replace-string-at-position
import replaceStringAtPosition from 'replace-string-at-position';

console.log(replaceStringAtPosition('hello world', 'world', 'there', 6));

prints:

hello there

The current version is 2.0.0, published on npm on 2026-09-26 (UTC). It needs Node 20 or later and ships an ES module build, a CommonJS build and TypeScript declarations for both.

The pages

  • Getting started: installing it, importing it from ES modules, CommonJS, TypeScript (each module setting, each TypeScript line), Bun and Deno.
  • API reference: the signature, the exports of each build, what it returns and what it throws.
  • How text is replaced: the exact rule, with every boundary: empty strings, the end of the text, emoji, String objects.
  • Edge cases and errors: every error message, the order the arguments are checked in, and what counts as a string or a number.
  • Recipes: one regular-expression match, every match, removing text, guarding against a stale position, code point positions, several edits.
  • Versions and upgrading: the releases, and moving from 1.0.4 to 2.0.0 with the same calls run on both.
  • FAQ: short answers to the questions the package raises.
  • Development: building, testing and releasing it.

Every output on these pages was printed by the published 2.0.0 from npm, on Node 24.18.0 and again on Node 20.20.2 with the same result.

Elsewhere

Clone this wiki locally