stream-replace

replace text in a stream

Usage no npm install needed!

<script type="module">
  import streamReplace from 'https://cdn.skypack.dev/stream-replace';
</script>

README

stream-replace

Build Status

Returns a transform stream that outputs data with 'needle' replaced with 'replacer'.

Features

streamReplace(regexp|substr, newSubStr|function)

  • Returns a Stream.Transform instance, whose output has some or all matches of a pattern in the stream's input replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. See String.prototype.replace().

Examples

// Repalce all occurrences of 'foo' in 'data.txt' with 'bar',
// and output to stdout

var fs = require('fs');
var replace = require('stream-replace');

fs.createReadStream('data.txt')
  .pipe(replace(/foo/g, 'bar'))
  .pipe(process.stdout);
// Create a server listening on port 3000 that
// proxies www.example.com, and replaces
// 'example' with 'zombocom', and 'domain' with 'web page'

var replace = require('stream-replace');
var http = require('http');

http.createServer(function handler(req, res) {
  http.request('http://www.example.com/', function onResponse(response) {
    response
      .pipe(replace(/example/ig, 'zombocom'))
      .pipe(replace(/domain/ig,  'web page'))
      .pipe(res);
  }).end();
}).listen(3000);

License

MIT