Currently, I am working on a challenge involving replacing strings using a function that accepts a string and an object of values.
This task involves a two-part algorithm:
- Replacing values within the string that are enclosed in braces.
- If the value is within double braces, erase the outer braces as they represent escaped sequences.
For example, consider the following test case:
expect(replaceValue('The sky is [condition] and the color is [[blue]]', {'condition':'clear')).toBe('The sky is clear and the color is [blue]');
I successfully managed to create the first part of the solution:
function replaceValue(input, replacementValue){
let copied = input;
Object.keys(replacementValue).forEach(ele => {
copied = copied.replace(`[${ele}]`, replacementValue[ele]);
})
return copied;
}
However, I am currently facing difficulties with implementing the second part which involves removing the outer braces.