replace with a string will only replace the first occurrence of that string. You pass an empty string, which is found at the beginning of the string ("something".indexOf("") returns 0).
Between the beginning of the string and the first character, there is nothing. Same goes for anything between characters. The empty string can be found between any characters (you can do the same what Alex did, e.g. "foo" + "" + "bar" === "foobar").
To further illustrate, here's what happens, if you replace all empty/zero-length strings in str: str.replace(new RegExp("", "g"), "-") → "-V-i-s-i-t- -M-i-c-r-o-s-o-f-t-!-"
replacewith a string will only replace the first occurrence of that string. You pass an empty string, which is found at the beginning of the string ("something".indexOf("")returns0)."Visit Microsoft!" === "" + "Visit Microsoft!""foo" + "" + "bar" === "foobar").str:str.replace(new RegExp("", "g"), "-")→"-V-i-s-i-t- -M-i-c-r-o-s-o-f-t-!-"