README
frep
Find, replace and string tranformation utility for node.js. Modify strings by passing an array or object of RegExp or string replacement patterns. Patterns can be strings, arrays of strings or regex, replacements can be strings or functions.
Quickstart
Install with npm:
npm i frep --save-dev
Usage:
var replace = require('frep');
// Patterns can be strings, regex or arrays.
// Replacements can be strings or functions.
var replacements = [
{
pattern: 'a',
replacement: 'x'
},
{
pattern: /b/,
replacement: 'y'
},
{
pattern: /c[\S]+/,
replacement: function(match) {
return match.toUpperCase();
}
}
];
console.log(replace.strWithArr('abcdefg', replacements));
//=> 'xyCDEFG'
API
// Transform a string with an array of replacement patterns
replace.strWithArr(String, replacements);
// Transform an array of strings with an array of replacement patterns
replace.arrWithArr(Array, replacements);
// Transform a string with an object of replacement patterns
replace.strWithObj(String, replacements);
// Transform an array of strings with an object of replacement patterns
replace.arrWithObj(Array, replacements);
.strWithArr( string, array )
Transform a string with an array of replacement patterns.
Parameters:
String
: The string to modify with the given replacement patterns.Array
: Array of objects containing the replacement patterns, each including apattern
property (which can be a string or a RegExp), and areplacement
property (which can be a string or a function to be called for each match).- A new string is returned with some or all matches replaced by the given replacement patterns.
Example 1
Given the following:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var patterns = [
{
pattern: /[ABC]/g,
replacement: '###'
},
{
pattern: /[XYZ]/g,
replacement: '$