frep 
A find and replace utility. Modify strings by passing an array of RegExp or string replacement patterns
Quickstart
npm i frep --save
var frep = require('frep');
frep.strWithArr(String, replacements);
frep.arrWithArr(Array, replacements);
frep.strWithObj(String, replacements);
frep.arrWithObj(Array, replacements);
Methods
.strWithArr( string, array )
Transform a string with an array of replacement patterns.
frep.strWithArr(String, Array)
Parameters:
String
: The string to modify with the given replacement patterns.
Array
: Array of objects containing the replacement patterns, each including a pattern
property (which can be a string or a RegExp), and a replacement
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: '$$$'
},
...
];
frep.strWithArr(str, patterns));
patterns as arrays
Patterns may also be arrays. When replacement patterns are formatted as arrays Frep will first transform the array into a corresponding RegExp group:
Example 2
['[ABC]', '[XYZ]']
gets converted to:
/([ABC]|[XYZ])/gi
Example 3
So the following will produce a similar result to Example 1, except ###
is used to replace all patterns:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var patterns = [
{
pattern: ['[ABC]', '[XYZ]'],
replacement: '###'
}
];
frep.strWithArr(str, patterns));
.arrWithArr( array, array )
Transform an array of strings with an array of replacement patterns
frep.arrWithArr( Array, Array )
Parameters:
Array
: The string to modify with the given replacement patterns.
Array
: Same as replacStr
, this is an an array of objects containing the replacement patterns, each including a pattern
property, which can be a string or a RegExp, and a replacement
property, which can be a string or a function to be called for each match.
- A new array of strings is returned with some or all matches replaced by the given replacement patterns.
Given the following:
Example 4
var arr = [
'Jon Schlinkert',
'Brian Woodward'
];
var patterns = [
{
pattern: /(B|S)/g,
replacement: '###'
},
{
pattern: /(J|W)/g,
replacement: '$$$'
},
...
];
frep.arrWithArr(arr, patterns));
An array of new strings is returned, with some or all matches in each string replaced by the given replacement strings.
.strWithObj( string, object )
Transform a string with an object of replacement patterns
frep.strWithObj( String, Object )
Parameters:
String
: The string to modify with the given replacement patterns.
Object
: Object of replacement patterns, where each key is a string or a RegExp pattern
, and each value is the replacement
string or function to be called for each match.
- A new string is returned with some or all matches replaced by the given replacement patterns.
Example 5
Given the following:
var str = 'ABC'
var replacements = {
'A': 'AAA',
'B': 'BBB',
'C': 'CCC',
'D': 'DDD',
'E': 'EEE',
'F': 'FFF'
};
frep.strWithObj(str, replacements));
.arrWithObj( array, object )
Transform an array of strings with an object of replacement patterns
frep.arrWithObj(Array, Object)
Parameters:
Array
: The array of strings to modify with the given replacement patterns.
Object
: Object of replacement patterns, where each key is a string or a RegExp pattern
, and each value is the replacement
string or function to be called for each match.
- A new array of strings is returned with some or all matches replaced by the given replacement patterns.
Example 6
Given the following:
var arr = ['ABC', 'DEF'];
var replacements = {
'A': 'AAA',
'B': 'BBB',
'C': 'CCC',
'D': 'DDD',
'E': 'EEE',
'F': 'FFF'
};
frep.arrWithObj(arr, replacements));
Usage example
Uses frep.strWithArray( string, array )
Slugify URL segments using frep
To run the example, first do: npm install frep underscore.string
var frep = require('frep');
var _str = require('underscore.string');
var slugger = function(str) {
return str.replace(/( |-|\.)/g, '_').toLowerCase();
};
var sluggifier = function(str) {
return str.replace(/( |\.)/g, '-');
};
var obj = {
foo: 'This is foo.',
bar: 'ThIs iS bAr.',
baz: 'THIS is BAZ.',
};
var patterns = [
{
pattern: /:foo/g,
replacement: _str.slugify(obj.foo)
},
{
pattern: /:bar/g,
replacement: slugger(obj.bar)
},
{
pattern: /:baz/g,
replacement: sluggifier(obj.baz)
}
];
console.log(frep.strWithArr(':foo/:bar/:baz', patterns));
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Licensed under the MIT license.