How to remove a specific character from a string and put it back later when needed

For example

let myString = "This is my string";

let replacedString = myString.replace(/ /g, "") //Thisismystring

Now that all the whitespaces have been removed, how do I put them back in the exact position?

Additionally, let’s suppose the replaced string undergoes some change and becomes

let myChangedString = "(T)(h)(i)(s)(i)(s)(m)(y)(s)(t)(r)(i)(n)(g)";

Now I want to put the whitespaces back where they used to be i.e after (s) and before (i), after (s) and before (m), after (y) before (s)

I’ve spent a couple of hours on this and been stuck in the same position, any form of help would be greatly appreciated.

EDIT: Solved, thank you very much.

Answer

Better you just transform your original array. Loop through array and modify the char is not empty.

let myString = "This is my string";

let chars = [...myString].map(item => item !== ' ' ? '(' + item + ')':  item)

console.log(chars.join(''))