JSFiddle - React, Tailwind, and code Playground

by Anurag Udasi

HTML

<!-- STRING REPLACE OCCURENCES -->

JavaScript

function replace_occurences(str, from, to)
{
    if(str.length == 0 || from.length == 0 || str.length < from.length)
        return str;
    else
    {
        if(str.substring(0, from.length) == from) // we gotta replace
            return to  + replace_occurences(str.substring(from.length), from, to);
        else
            return str[0] + replace_occurences(str.substring(1) ,from, to);
    }
};

alert(replace_occurences("i am indian", "i", "indian"));