sed-lite

raw JSON →
1.1.0 verified Fri May 01 auth: no javascript

JavaScript implementation of the sed command-line tool for text substitution. Current stable version 1.1.0 compiles sed expressions to functions, supporting flags like g (global) and custom delimiters. The library is lightweight, has no dependencies, and ships TypeScript declarations. It differs from alternatives like xregexp by focusing on sed syntax compatibility, enabling use in Node.js and browsers.

error SyntaxError: Invalid regular expression: /s/(foo)/(bar)//: Nothing to repeat
cause Passing a sed command string with slashes without proper escaping or incorrect syntax.
fix
Ensure the sed string is correctly formatted: e.g., 's/foo/bar/' — use valid JavaScript RegExp patterns. If using custom delimiter, use a character not appearing in the pattern or replacement, e.g., 's#foo#bar#'.
error TypeError: sed is not a function
cause Incorrect import: default import vs named import mismatch.
fix
Use import { sed } from 'sed-lite' (named) or import sed from 'sed-lite' (default), depending on how you want to call it.
error sed(...) is not a function
cause sed() returns a function that takes a string argument. Using it directly as a replacement function (e.g., String.prototype.replace(sed(...))) is invalid.
fix
Call sed(expression)(input) or store the compiled function in a variable.
gotcha sed-lite does not support all GNU sed features; only 's' command (substitute) is implemented. Attempting other commands like 'd', 'p', 'a', 'i' may throw or silently fail.
fix Use only 's' command. For extended functionality, consider other libraries like xregexp or manual RegExp.
gotcha Regular expression syntax follows JavaScript RegExp, not sed's BRE/ERE. Escaping patterns may differ from standard sed (e.g., back-references use $1 instead of \1).
fix Use JavaScript regex syntax: e.g., 's/(\w+)/$1/' — note the double escaping in string literal.
gotcha Flags other than 'g' (global) are not supported (e.g., 'i' for case-insensitive, flags for multiple replacement with 'N' substitution). Attempting unknown flags may be ignored or cause unexpected behavior.
fix To mimic 'i' flag, modify regex pattern manually: e.g., 's/pattern/replacement/i' in string but sed-lite does not interpret 'i'. Instead use native RegExp: str.replace(/pattern/i, 'replacement').
deprecated The package exports a default function and named 'sed'. The default export may be deprecated in future; prefer named import.
fix Use import { sed } from 'sed-lite' instead of import sed from 'sed-lite'.
npm install sed-lite
yarn add sed-lite
pnpm add sed-lite

Demonstrates basic usage: compile sed substitution, global flag, custom delimiter, and chaining expressions.

import { sed } from 'sed-lite';

const transform = sed('s/world/JavaScript/');
const result = transform('Hello, world!');
console.log(result); // Hello, JavaScript!

// With global flag
const replaceAll = sed('s/\d+/###/g');
console.log(replaceAll('Item 1, Item 2, Item 3')); // Item ###, Item ###, Item ###

// Custom delimiter
const changeFormat = sed('s#2024-01-01#01/01/2024#');
console.log(changeFormat('Date: 2024-01-01')); // Date: 01/01/2024

// Chain multiple expressions with semicolon
const multi = sed('s/foo/bar/; s/bar/baz/');
console.log(multi('foo')); // baz