Here's one approach:
function encodeQuotesOccuringAfter(string, substring) {
if(string.indexOf(substring) == -1) {
return string;
}
var all = string.split(substring);
var encoded = [all.shift(), all.join(substring).replace(/"/g, """)];
return encoded.join(substring)
}
This second one is a bit wasteful, but you could move this into a function, and compute startAt
only once. Idea is to look for all quotes, and only change the ones that have "H1:" appear before them.
str.replace(/"/g, function(match, offset, string) {
var startAt = string.indexOf("H1:");
if(startAt != -1 && offset > startAt) {
return """;
}
else {
return '"';
}
});
Using our domain knowledge that H1:
does not contain quotes, we simply do a replace on the entire string.
str.replace(/"/g, """);