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

JavascriptJqueryRegexString

Javascript Problem Overview


I have a filename that can have multiple dots in it and could end with any extension:

tro.lo.lo.lo.lo.lo.png

I need to use a regex to replace the last occurrence of the dot with another string like @2x and then the dot again (very much like a retina image filename) i.e.:

tro.lo.png -> tro.lo@2x.png

Here's what I have so far but it won't match anything...

str = "http://example.com/image.png";
str.replace(/.([^.]*)$/, " @2x.");

any suggestions?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Just use special replacement pattern $1 in the replacement string:

console.log("tro.lo.lo.lo.lo.lo.png".replace(/\.([^.]+)$/, "@2x.$1"));
// "[email protected]"

Solution 3 - Javascript

You can use the expression \.([^.]*?):

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

You need to reference the $1 subgroup to copy the portion back into the resulting string.

Solution 4 - Javascript

working demo http://jsfiddle.net/AbDyh/1/

code

var str = 'tro.lo.lo.lo.lo.lo.zip',
    replacement = '@2x.';
str = str.replace(/.([^.]*)$/, replacement + '$1');

$('.test').html(str);

alert(str);
ā€‹

Solution 5 - Javascript

To match all characters from the beginning of the string until (and including) the last occurence of a character use:

^.*\.(?=[^.]*$)  To match the last occurrence of the "." character

^.*_(?=[^.]*$)   To match the last occurrence of the "_" character

Solution 6 - Javascript

Use \. to match a dot. The character . matches any character.

Therefore str.replace(/\.([^\.]*)$/, ' @2x.').

Solution 7 - Javascript

You could simply do like this,

> "tro.lo.lo.lo.lo.lo.zip".replace(/^(.*)\./, "$1@2x");
'tro.lo.lo.lo.lo.lo@2xzip'

Solution 8 - Javascript

Why not simply split the string and add said suffix to the second to last entry:

var arr = 'tro.lo.lo.lo.lo.lo.zip'.split('.');
arr[arr.length-2] += '@2x';
var newString = arr.join('.');

Solution 9 - Javascript

'tro.lo.lo.lo.lo.lo.png'.replace(/([^\.]+).+(\.[^.]+)/, "$1.@x2$2")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaltView Question on Stackoverflow
Solution 1 - JavascriptJonView Answer on Stackoverflow
Solution 2 - JavascriptSalman AView Answer on Stackoverflow
Solution 3 - Javascriptuser193476View Answer on Stackoverflow
Solution 4 - JavascriptTats_innitView Answer on Stackoverflow
Solution 5 - JavascriptJRSView Answer on Stackoverflow
Solution 6 - JavascriptlinepoglView Answer on Stackoverflow
Solution 7 - JavascriptAvinash RajView Answer on Stackoverflow
Solution 8 - JavascriptJ_A_XView Answer on Stackoverflow
Solution 9 - JavascriptJaimeView Answer on Stackoverflow