Skip to content

fast-replaceall

npm package

NPM versionNPM Downloadsjsdelivr

High-performance string replacement tool
Supports global replacement, case-insensitive mode, start index control, and functional replacement compatible with native String.replace.


Features 🌟

  • Global Replacement: Achieve full replacement without regular expressions
  • Flexible Options: Customize with caseInsensitive and fromIndex parameters
  • Functional Replacement: Use callback functions for dynamic logic
  • TypeScript Support: Full type definitions included
  • Lightweight: Only 1KB (uncompressed), zero dependencies

Installation 💻

bash
npm add fast-replaceall
bash
pnpm add fast-replaceall
bash
yarn add fast-replaceall
html
<script src="https://cdn.jsdelivr.net/npm/fast-replaceall/dist/index.umd.min.js"></script>
<script>
  const text = 'The quick brown fox jumps over the lazy dog';
  console.log(FastReplaceall(text, 'dog', 'monkey'));
  // Output: The quick brown fox jumps over the lazy monkey
</script>

Usage Examples 🚀

Basic Replacement

javascript
import replaceAll from 'fast-replaceall';

const text = 'The quick brown fox jumps over the lazy dog';
console.log(replaceAll(text, 'dog', 'monkey'));
// Output: The quick brown fox jumps over the lazy monkey

Functional Replacement

javascript
replaceAll('123-456', '-', (match, offset) => `_${offset}_`);
// Output: 123_0_456

Custom Options

javascript
// Case-insensitive replacement
replaceAll('Apple apple', 'APPLE', 'ORANGE', { caseInsensitive: true });
// Output: ORANGE ORANGE

// Start replacing from index 2
replaceAll('aaaa', 'a', '*', { fromIndex: 2 });
// Output: aa**

API Documentation 📖

Function Signature

typescript
export default function replaceAll(
  str: string,
  substr: string,
  replacement: string | ReplacementFn,
  options?: ReplaceAllOptions
): string;

Parameters

ParameterTypeDescription
strstringOriginal string to operate on
substrstringSubstring to match
replacement`stringReplacementFn`
optionsReplaceAllOptionsOptional configuration parameters

Configuration Options

typescript
interface ReplaceAllOptions {
  fromIndex?: number; // Start index (default 0, supports negative values)
  caseInsensitive?: boolean; // Case-insensitive matching (default false)
}

type ReplacementFn = (match: string, offset: number, str: string) => string;

Project Inspiration

Enhanced over native String.replace to address:

  • No regex required: Direct string-based matching
  • Intuitive parameters: Replace complex regex flags with clear options
  • Edge-case handling: Support special empty-string replacement (configurable error throwing or formatted output)

Comparison with Native Methods 🎯

Requirementfast-replaceallString.prototype.replace
Global ReplacementBuilt-in supportRequires /g flag in regex
Case-insensitivecaseInsensitive optionRequires i flag in regex
Start Index ControlfromIndex parameterRequires manual string slicing
Functional ReplacementFull supportRequires regex + function
text
======== Benchmark Results ========

【String.prototype.replace】 x 6,635,525 ops/sec ±2.58% (87 runs sampled)
【String.prototype.replaceAll】 x 5,636,302 ops/sec ±0.58% (94 runs sampled)
【replaceAll】 x 40,483,612 ops/sec ±0.67% (94 runs sampled)
The fastest is 【replaceAll】

======== Benchmark End ========

Last updated: