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
    180
    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
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,424
    Thanks
    26
    Thanked 748 Times in 746 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.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

  • #3
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,424
    Thanks
    26
    Thanked 748 Times in 746 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.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

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

    Code:
    "xxx.abc.xyz.gif".split(".").pop()
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  • #5
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,424
    Thanks
    26
    Thanked 748 Times in 746 Posts
    split is deprecated.
    Evolution - The non-random survival of random variants.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,608
    Thanks
    11
    Thanked 619 Times in 599 Posts
    Quote Originally Posted by sunfighter View Post
    split is deprecated.
    where on earth did you get that idea from?
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,435
    Thanks
    85
    Thanked 4,912 Times in 4,873 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('');
    Be yourself. No one else is as qualified.

  • #8
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,424
    Thanks
    26
    Thanked 748 Times in 746 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.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

  • #9
    Regular Coder
    Join Date
    Apr 2007
    Posts
    180
    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
    •