Find Replace Feedback Loop Problem
If find replace pairs is done one after one, then you may end up with a substring that's not in the original input string nor in any of the find replace pairs. For example.
Input string: abcd
replace pairs:
a
→c
c
→d
Result:
- Expected result:
cbdd
- Wrong result:
dbdd
Real World Example
JavaScript Example
const xx = "abcd"; const xpairs = [ ["a", "c"], ["c", "d"], ]; const xresult = xpairs.reduce((a, b) => (a.replaceAll(b[0], b[1])), xx); console.log(xresult === "dbdd");
WolframLang Example
WolframLang's find-replace function does not have this feedback problem.
StringReplace[ "abcd", {"a" -> "c","c" -> "d"} ] === "cbdd"