RegEx that will match the last occurrence of dot in a string

You do not need a regex for this. String.lastIndexOf will do.

var str="tro.lo.lo.lo.lo.lo.zip";
var i = str.lastIndexOf('.');
if (i != -1) {
    str = str.substr(0, i) + "@2x" + str.substr(i);
}

See it in action.

Update: A regex solution, just for the fun of it:

str = str.replace(/\.(?=[^.]*$)/, "@2x.");

Matches a literal dot and then asserts ((?=) is positive lookahead) that no other character up to the end of the string is a dot. The replacement should include the one dot that was matched, unless you want to remove it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)