I have the current function:
function replaceValue(v) {
const firstRegex = new RegExp('hello', 'g');
const secondRegex = new RegExp('bye', 'g');
return v.replace(firstRegex, '@').replace(secondRegex, '#');
}
However, now I want to add even more regex, and I want to have a datastructure that looks like this:
const regexStorage = [{
value: '@',
replace: 'hello',
}, {
value: '#',
replace: 'bye',
}]
I can't figure out a way to use that regexStorage to dynamically add as many replaces as they exist in the array for a given value.
I've got to this:
function replaceValue(v) {
const firstRegex = new RegExp(regexStorage[0].replace, 'g');
const secondRegex = new RegExp(regexStorage[1].replace, 'g');
return v.replace(firstRegex, regexStorage[0].value).replace(secondRegex, regexStorage[1].value);
}
But that's just using the storage. I wonder how I can do this dynamically.