How can I insert a string at a specific index of another string?
var txt1 = \"foo baz\"
Suppose I want to insert \"bar \" after the \"foo
You can do it easily with regexp in one line of code
const str = 'Hello RegExp!'; const index = 6; const insert = 'Lovely '; //'Hello RegExp!'.replace(/^(.{6})(.)/, `$1Lovely $2`); const res = str.replace(new RegExp(`^(.{${index}})(.)`), `$1${insert}$2`); console.log(res);
"Hello Lovely RegExp!"