Nested Replace
Find a specified string or a regular expression in a string, or a nested array/object value) and returns a new value where the specified string are replaced.
An Example usage of this package could be when you need to replace a value in entire a big json
Installation
$ npm install nested-replace
Usage
const nestedReplace = require('nested-replace');
const str = 'this is a string value';
const newStr = nestedReplace(str, 'is', 'XX');
const newStr = nestedReplace(str, /is/g, 'XX');
const str = '0123456789';
const newStr = nestedReplace(/[0-9]/g, match => match % 2 ? 'x' : 'y')
And finally the most interesting part is that you can do all of this kind of things with any kind of nested array and/or object
const input = {
a: 'this is a string value',
b: [
'this is a string value inside an array',
[
'this is a string value inside a nested array'
],
{
c: {
d: [
{
e: 'this is a string value inside a nested object which is inside a nested array which is inside a nested object which is inside another nested object :)'
}
]
}
}
],
};
const newInput = nestedReplace(input, /is/g, 'XX');
Note: This function always returns a new value and does not modify the input value
If you do not use regex for replace all, it only replaces the first found value in each string value in entire object
For example:
const newInput = nestedReplace(input, 'is', 'XX');