Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    trying to do is to match and catch the END characters

    var str = "xxx.abc.xyz.gif";
    var patt = /\..+$/i;
    var result = patt.exec(str);

    What I am trying to do is to match and catch the END characters after the last ".", such as "gif".

    Example:
    var str = "aaaa.bbbb.cccc.tiff";

    I want to match and catch the "tiff" only so that the result will return "tiff".

    How should I write the pattern expression?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    Wouldn't this be simpler:
    Code:
    <script type="text/javascript">
    var str = "xxx.abc.xyz.gif";
    var n = str.substr(str.length-3,3);
    document.write(n);
    </script>
    Evolution - The non-random survival of random variants.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    OK doesn't work for extensions over 3 in length. This will do it for any length.

    Code:
    <script type="text/javascript">
    var str = "xxx.abc.xyz.giftyalltheway";
    var rts = str.split('').reverse().join('');
    var n = str.substr(str.length-rts.indexOf('.'),rts.indexOf('.'));
    document.write(n);
    </script>
    Evolution - The non-random survival of random variants.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    a much easier/faster way of doing it:

    Code:
    "xxx.abc.xyz.gif".split(".").pop()
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    split is deprecated.
    Evolution - The non-random survival of random variants.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by sunfighter View Post
    split is deprecated.
    where on earth did you get that idea from?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Even if split were deprecated (and if it is there are one heluva lot of scripts out there that are in desperate trouble!), you could still do it much simpler via
    Code:
    var imagename = "xxx.abc.xyz.gif";
    var imagetype = imagname.substring( imagename.lastIndexOf(".") + 1 );
    No need for all the silly reverse stuff.

    Come to think of it, if split is deprecated, then YOUR answer wouldn't work:
    Code:
    var rts = str.split('').reverse().join('');
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    @rnd me, Sorry, ran your code in a php block and that came up
    Always associate pop with an array not a string. And yes I see you turned the string into an array. Pretty sneaky, I think I did that too.
    Evolution - The non-random survival of random variants.

  • #9
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks for the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •