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

104,348

Solution 1

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

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

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 4

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 5

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

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

Share:
104,348
alt
Author by

alt

Updated on July 05, 2022

Comments

  • alt
    alt almost 2 years

    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 -> [email protected]
    

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

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

    any suggestions?

  • alt
    alt almost 12 years
    That returns an integer of the position where that character is, right?
  • alt
    alt almost 12 years
    How can I use that integer with .replace() ?
  • Waihon Yew
    Waihon Yew almost 12 years
    @JacksonGariety: You don't need to. String.substr will round things up.
  • gdoron is supporting Monica
    gdoron is supporting Monica almost 12 years
    Try your code: "tro.lo.lo.lo.lo.lo.zip".replace(/\.([^\.]*)$/, ' @2x.') It doesn't work. Sorry.
  • alt
    alt almost 12 years
    Cool answer! Why is this better/more efficient/less code than a regex?
  • gdoron is supporting Monica
    gdoron is supporting Monica almost 12 years
    Not quite. the output is missing a dot.
  • alt
    alt almost 12 years
    Cool, did not know that about regex
  • gdoron is supporting Monica
    gdoron is supporting Monica almost 12 years
    @JacksonGariety. It's a lot more readable, and little bit faster.
  • gdoron is supporting Monica
    gdoron is supporting Monica almost 12 years
    Output should be [email protected] You output tro.lo.lo.lo.lo.lo@2xzip
  • Tats_innit
    Tats_innit almost 12 years
    @gdoron lol 35 second difference :) cheers updated version here jsfiddle.net/AbDyh/1
  • Tats_innit
    Tats_innit almost 12 years
    @gdoron you da man - by the way I was thinking when you will apply for moderator role man! anyhow SO is not chat applet but just a recommendation!
  • gdoron is supporting Monica
    gdoron is supporting Monica almost 12 years
    Not soon... I don't want the job, nor have the time. Cheers. :)
  • Qtax
    Qtax almost 12 years
    Correctly answers OPs question.
  • Tats_innit
    Tats_innit almost 12 years
    @gdoron o well if you change your mind you will have my ++1 recommendation with best comment! :)
  • Waihon Yew
    Waihon Yew almost 12 years
    @Qtax: Not quite, because it won't work if the last dot is also the very last character.
  • Waihon Yew
    Waihon Yew almost 12 years
    @JacksonGariety: Cool, thanks. I also added a regex solution for completeness.
  • Qtax
    Qtax almost 12 years
    @Jon, true. Almost korrect then. ;) But /\.([^.]*)$/ does it, no need for lookarounds.
  • alt
    alt almost 12 years
    Heya, quick new question for the RegExpert... How would I best remove the beginning 'url(' and closing ')' around a url without removing them from elsewhere in the string? Thanks.
  • Waihon Yew
    Waihon Yew almost 12 years
    You 'd match "url\((-url-regex-here-)\)" and replace with "$1", which would leave only the url. -url-regex-here- is a regular expression that matches urls; google for that, for a quick and dirty solution that mostly works you could use [^)]+.
  • Salman A
    Salman A almost 12 years
    I am not sure if a filename could have a . as the very last character.