join result that is separated by a diffrent regex match

hello im doing something like how to replace dots inside quote in sentence with regex

var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. "Vestibulum interdum dolor nec sapien blandit a suscipit arcu fermentum. Nullam lacinia ipsum vitae enim consequat iaculis quis in augue. Phasellus fermentum congue blandit. Donec laoreet, ipsum et vestibulum vulputate, risus augue commodo nisi, vel hendrerit sem justo sed mauris." Phasellus ut nunc neque, id varius nunc. In enim lectus, blandit et dictum at, molestie in nunc. Vivamus eu ligula sed augue pretium tincidunt sit amet ac nisl. "Morbi eu elit diam, sed tristique nunc."';

// seperate the quotes
var quotes = string.match(/"(.)+?"/g);
var test = [];

// for each quotes
for (var i = quotes.length - 1; i >= 0; i--) {

    // replace all the dot inside the quote
    test[i] = quotes[i].replace(/\./g, '[dot]');
};

console.log(test);

lets say we already make the change with the regex, but im stuck at how can we join it back to the existing var string as my result is seperated in var test ? or theres a better way?

the output should be something like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. "Vestibulum interdum dolor nec sapien blandit a suscipit arcu fermentum[dot]Nullam lacinia ipsum vitae enim consequat iaculis quis in augue[dot] Phasellus fermentum congue blandit[dot] Donec laoreet, ipsum et vestibulum vulputate, risus augue commodo nisi, vel hendrerit sem justo sed mauris[dot]" Phasellus ut nunc neque, id varius nunc. In enim lectus, blandit et dictum at, molestie in nunc. Vivamus eu ligula sed augue pretium tincidunt sit amet ac nisl. "Morbi eu elit diam, sed tristique nunc[dot]"

*ps im not sure the title is corrent

thanks

You could instead just split at ", do the replacement in every second array element, then join again:

var parts = string.split('"');
for (var i = 1; i < parts.length; i += 2) {
    parts[i] = parts [i].replace(/\./g, '[dot]');
};
string = parts.join('"');

Since split will create an empty string at index 0 if the string starts with " this should work in all cases.

Note that the edge case of a trailing unmatched " will lead to every dot after that " to be replaced as well. If you do not want this, simply change the for condition to i < parts.length - 1.

JSFiddle Demo

Use regexp but with replace function:

string.replace(/"[^"]+"/g, function(m) {return m.replace(/\./g,"[dot]")})