beraliv

Replace all occurrences in a string

Example of ReplaceAll use
Example of ReplaceAll use

Today we discuss ReplaceAll

We need to remove all occurrences in the string and place instead the different value

Let's start ⏳

Replace all occurrences

Let's check the solution for Replace and adapt it for ReplaceAll:

Replace, solution
Replace, solution

This solution substitutes From for To once: https://tsplay.dev/mA7ZXw

Let's do that as many times as required:

ReplaceAll, version 1
ReplaceAll, version 1

But if we call ReplaceAll recursively this way, we will do it incorrectly. Let me give an example:

Not working example for version 1
Not working example for version 1

So instead of calling ReplaceAll for the whole string, let's do it for the rest of the string, or End:

Solution
Solution

Thank you for your time! ⌛️

The final solution with test cases are available here: https://tsplay.dev/mplJBm

Have a wonderful evening 👩‍💻

typescript
Alexey Berezin profile image

Written by Alexey Berezin who loves London 🏴󠁧󠁢󠁥󠁮󠁧󠁿, players ⏯ and TypeScript 🦺 Follow me on Twitter