How to replace a substring between two indices

javascript, jquery

Solution

There is no such method in JavaScript. But you can always create your own:

String.prototype.replaceBetween = function(start, end, what) {
  return this.substring(0, start) + what + this.substring(end);
};

console.log("The Hello World Code!".replaceBetween(4, 9, "Hi"));

Problem

I want to replace text between two indices in Javascript, something like: ``` str = "The Hello World Code!"; str.replaceBetween(4,9,"Hi"); // outputs "The Hi World Code" ``` The indices and the string are both dynamic. How could I go about doing this?

Original source