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 apatternproperty (which can be a string or a RegExp), and areplacementproperty (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: '$
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: '###'
}
];
replace.strWithArr(str, patterns));
// => #########DEFGHIJKLMNOPQRSTUVW#########
.arrWithArr( array, array )
Transform an array of strings with an array of replacement patterns
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: '$
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
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'
};
replace.strWithObj(str, replacements));
// => AAABBBCCC
.arrWithObj( array, object )
Transform an array of strings with an object of replacement patterns
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'
};
replace.arrWithObj(arr, replacements));
// => ['AAABBBCCC', 'DDDEEEFFF']
Usage example
replace.strWithArray( string, array )
Slugify URL segments using frep
To run the example, first do: npm install frep underscore.string
var replace = require('frep');
// We'll use underscore string's slugify function for the first example
var _str = require('underscore.string');
// A custom slugification function for the second
var slugger = function(str) {
return str.replace(/( |-|\.)/g, '_').toLowerCase();
};
// And a third slugification function for the last example
var sluggifier = function(str) {
return str.replace(/( |\.)/g, '-');
};
// This is an object of data, where each property will be used
// to build up a URL that needs to be slugified. e.g.
// => /foo/bar/baz
// (in reality, you would probably have an array of objects like this)
var obj = {
foo: 'This is foo.',
bar: 'ThIs iS bAr.',
baz: 'THIS is BAZ.',
};
// Our custom replacement patterns. These are used to
// transform the data from each property
var patterns = [
{
pattern: /:foo/g,
replacement: _str.slugify(obj.foo) // underscore.string
},
{
pattern: /:bar/g,
replacement: slugger(obj.bar) // custom function #1
},
{
pattern: /:baz/g,
replacement: sluggifier(obj.baz) // custom function #2
}
];
// The first argument, a string, will be our "structure",
// which will determine where the values from each property
// will be placed. Run frep to see what happens!
console.log(replace.strWithArr(':foo/:bar/:baz', patterns));
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on May 14, 2014.
},
...
];
replace.strWithArr(str, patterns));
// => #########DEFGHIJKLMNOPQRSTUVW$$$$$
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
__CODE_BLOCK_4__
gets converted to:
__CODE_BLOCK_5__
Example 3
So the following will produce a similar result to Example 1, except __INLINE_CODE_4__ is used to replace all patterns:
__CODE_BLOCK_6__
.arrWithArr( array, array )
Transform an array of strings with an array of replacement patterns
Parameters:
- __INLINE_CODE_5__: The string to modify with the given replacement patterns.
- __INLINE_CODE_6__: Same as __INLINE_CODE_7__, this is an an array of objects containing the replacement patterns, each including a __INLINE_CODE_8__ property, which can be a string or a RegExp, and a __INLINE_CODE_9__ 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
__CODE_BLOCK_7__
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
Parameters:
- __INLINE_CODE_10__: The string to modify with the given replacement patterns.
- __INLINE_CODE_11__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_12__, and each value is the __INLINE_CODE_13__ 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:
__CODE_BLOCK_8__
.arrWithObj( array, object )
Transform an array of strings with an object of replacement patterns
Parameters:
- __INLINE_CODE_14__: The array of strings to modify with the given replacement patterns.
- __INLINE_CODE_15__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_16__, and each value is the __INLINE_CODE_17__ 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:
__CODE_BLOCK_9__
Usage example
replace.strWithArray( string, array )
Slugify URL segments using frep
To run the example, first do: __INLINE_CODE_18__
__CODE_BLOCK_10__
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on May 14, 2014.
},
...
];
replace.arrWithArr(arr, patterns));
// => ["$$on ###chlinkert", "###rian $$oodward"]
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
Parameters:
- __INLINE_CODE_10__: The string to modify with the given replacement patterns.
- __INLINE_CODE_11__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_12__, and each value is the __INLINE_CODE_13__ 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:
__CODE_BLOCK_8__.arrWithObj( array, object )
Transform an array of strings with an object of replacement patterns
Parameters:
- __INLINE_CODE_14__: The array of strings to modify with the given replacement patterns.
- __INLINE_CODE_15__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_16__, and each value is the __INLINE_CODE_17__ 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:
__CODE_BLOCK_9__Usage example
replace.strWithArray( string, array )
Slugify URL segments using frep
To run the example, first do: __INLINE_CODE_18__
__CODE_BLOCK_10__Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license
This file was generated by verb-cli on May 14, 2014.
}, ... ]; replace.strWithArr(str, patterns)); // => #########DEFGHIJKLMNOPQRSTUVW$$$$$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
__CODE_BLOCK_4__gets converted to:
__CODE_BLOCK_5__Example 3
So the following will produce a similar result to Example 1, except __INLINE_CODE_4__ is used to replace all patterns:
__CODE_BLOCK_6__.arrWithArr( array, array )
Transform an array of strings with an array of replacement patterns
Parameters:
- __INLINE_CODE_5__: The string to modify with the given replacement patterns.
- __INLINE_CODE_6__: Same as __INLINE_CODE_7__, this is an an array of objects containing the replacement patterns, each including a __INLINE_CODE_8__ property, which can be a string or a RegExp, and a __INLINE_CODE_9__ 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
__CODE_BLOCK_7__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
Parameters:
- __INLINE_CODE_10__: The string to modify with the given replacement patterns.
- __INLINE_CODE_11__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_12__, and each value is the __INLINE_CODE_13__ 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:
__CODE_BLOCK_8__.arrWithObj( array, object )
Transform an array of strings with an object of replacement patterns
Parameters:
- __INLINE_CODE_14__: The array of strings to modify with the given replacement patterns.
- __INLINE_CODE_15__: Object of replacement patterns, where each key is a string or a RegExp __INLINE_CODE_16__, and each value is the __INLINE_CODE_17__ 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:
__CODE_BLOCK_9__Usage example
replace.strWithArray( string, array )
Slugify URL segments using frep
To run the example, first do: __INLINE_CODE_18__
__CODE_BLOCK_10__Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license
This file was generated by verb-cli on May 14, 2014.