replacements

Run multiple regex or string find-and-replace transformations on a string in sequence, reducing the final string to the accumulated result of all transformations.

replacements

Run multiple regex or string find-and-replace transformations on a string in sequence, reducing the final string to the accumulated result of all transformations.

npm

npm i replacements --save
var replace = require('replacements');
var transformers = {
  pattern: /a/g,
  replacement: 'bbb'
};
console.log(replace('aaa', transformers));
//=> 'bbbbbbbbb' 

For basic string transformations, you can pass the string to replace as the key, and the replacement as a value:

var transformers = {
  'a': 'b'
  'c': 'd'
};
console.log(replace('aaabbbccc', transformers));
//=> 'bbbbbbddd' 

Note that only the first matching string will only be replaced using this format. If you need more flexibility, use the pattern-replacement regex syntax..

var transformers = [
  {pattern: /a/g, replacement: 'bbb'},
  {pattern: /b/g, replacement: 'ccc'},
  {pattern: /c/g, replacement: 'ddd'},
  {pattern: /d/g, replacement: 'eee'},
  {pattern: /[e]+/g, replacement: '_DONE_'}
];
console.log(replace('aaa', transformers));
//=> '__DONE__' 
var transformers = {
  pattern: /a+/g,
  replacementfunction(match) {
    return match.split('').map(function(stri) {
      if (=== 0) {return 'b'; }
      if (=== 1) {return str.toUpperCase();}
      return str;
    }).join('');
  }
};
console.log(replace('aaa', transformers));
//=> 'bAa' 

Jon Schlinkert

Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license


This file was generated by verb-cli on July 05, 2014.