Find Replace Feedback Loop Problem

By Xah Lee. Date: . Last updated: .

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:

Result:

Real World Example

replace feedback problem 2023-01-11 n4vct
find-replace multi-pairs feedback problem 2023-01-11 n4vct

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");
JavaScript replace feedback 2023-01-11 6mPrh
JavaScript replace feedback 2023-01-11 6mPrh

WolframLang Example

WolframLang's find-replace function does not have this feedback problem.

StringReplace[ "abcd", {"a" -> "c","c" -> "d"} ] === "cbdd"
WolframLang replace feedback 2023-01-11
WolframLang replace feedback 2023-01-11