如何按范围替换字符串?

Dev*_*ile 12 javascript string replace

我需要用范围替换字符串示例:

string = "this is a string";//I need to replace index 0 to 3 whith another string Ex.:"that"
result = "that is a string";
Run Code Online (Sandbox Code Playgroud)

但这需要是恐怖的.不能替换固定的单词......需要按范围

我试过了

           result = string.replaceAt(0, 'that');
Run Code Online (Sandbox Code Playgroud)

但这只取代了第一个角色而我想要第一个到第三个角色

Ale*_*lov 23

function replaceRange(s, start, end, substitute) {
    return s.substring(0, start) + substitute + s.substring(end);
}

var str = "this is a string";
var newString = replaceRange(str, 0, 4, "that"); // "that is a string"
Run Code Online (Sandbox Code Playgroud)