I have been struggling the selectionStart and selectionEnd attributes of textarea to make them work with contenteditable d
This answer uses Selection#modify, which is non-standard, but at least, I suggest you to use "insertText" command:
function replaceVal(val, step) {
var selection = window.getSelection();
for (var i = 0; i < step; i += 1) {
selection.modify('extend', 'backward', 'character');
}
document.execCommand('insertText', false, val);
}
Test test test