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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    strip string to certain length

    i have a string that has the path of an image

    var str = "/somewhere/somewhere/image.jpg"


    how would i go about stripping that to only display "image.jpg" keep in mind that i will be using different images from different locations so i can't just do a right(str, 9) because this will be different each time. I need more of a way to select the right until the first "/"

    Thanks,
    ~Jamie

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var filename = str.replace(/.*\/(.*)/,'$1'));

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Mongus,

    Thanks for the reply.

    That didn't seem to work for me. I'm new to javascript so could your or anyone else explain to me what that does?

    Thanks,
    ~Jamie

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    var filename = str.replace(/.*\/(.*)/,'$1');

    mistyped an extra bracket
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    <script type="text/javascript">
    var str='/somewhere/somewhere/image.jpg'
    var filename = str.replace(/.*\/(.*)/,'$1');
    onload=function(){
    alert(filename)
    }
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you

    one last question... what if it was

    var str='\somewhere\somewhere\image.jpg'

    with "\" instead of "/"
    ??

  • #7
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks KOR.

    I copied from my test code and forgot to remove the trailing parenthesis.

  • #8
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should check for either forward or back slashes.
    Code:
    str.replace(/.*[\/\\](.*)/,'$1');


  •  

    Posting Permissions

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