function replaceLastOccurrence(s, subs, replace) {
const pos = s.lastIndexOf(subs);
if (pos === -1) {
return s;
}
s = s.slice(0, pos) + replace + s.slice(pos + subs.length);
return s;
}
const s = "node.js c++ c php c# node.js go node.js rust";
const result = replaceLastOccurrence(s, "node.js", "python");
console.log(result);
/*
run:
node.js c++ c php c# node.js go python rust
*/