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
    Missouri
    Posts
    5,326
    Thanks
    26
    Thanked 734 Times in 732 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.

    Soon to not be a resident of Missouri - hitting the road for better pastures.

  • #3
    Master Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    5,326
    Thanks
    26
    Thanked 734 Times in 732 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.

    Soon to not be a resident of Missouri - hitting the road for better pastures.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,600
    Thanks
    11
    Thanked 616 Times in 596 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
    Missouri
    Posts
    5,326
    Thanks
    26
    Thanked 734 Times in 732 Posts
    split is deprecated.
    Evolution - The non-random survival of random variants.

    Soon to not be a resident of Missouri - hitting the road for better pastures.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,600
    Thanks
    11
    Thanked 616 Times in 596 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,423
    Thanks
    85
    Thanked 4,908 Times in 4,869 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
    Missouri
    Posts
    5,326
    Thanks
    26
    Thanked 734 Times in 732 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.

    Soon to not be a resident of Missouri - hitting the road for better pastures.

  • #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
    •