for example:
var str=\"
hi
hi\";
to replace the last(second) ,
to change into \"<
You can use the fact that quantifiers are greedy:
str.replace(/(.*)
/, "$1");
But the disadvantage is that it will cause backtracking.
Another solution would be to split up the string, put the last two elements together and then join the parts:
var parts = str.split("
");
if (parts.length > 1) {
parts[parts.length - 2] += parts.pop();
}
str = parts.join("
");