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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Replace up to specific character in URL - javascript

    Greetings,

    I am having trouble finding any examples to help me strip everything up to a certain character (the hypen) in a url.

    Hypothetical example below, as I only know how to accomplish this using a literal only for one url at a time but I need this for changing urls:


    Code:
    <script language="JavaScript"><!--
    var temp = new String('http://www.site.com/abc_123_abc_-1379820930');
    temp = temp.replace("http://www.site.com/abc_123_abc_-", '');
    document.write(temp);
    //--></script>
    And I am hoping to be left with the series of numbers on the end. Any help is greatly appreciated.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You could combine indexOf() with substr()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Or use a regex:-

    Code:
    <script type = "text/javascript">
    var temp = new String('http://www.site.com/abc_123_abc_-1379820930');
    temp = temp.replace(/[\w\W]+\-/g,"");
    alert (temp);
    </script>
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3. If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

  • Users who have thanked Philip M for this post:

    bluetitan (12-06-2009)

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome, thats exactly what I was looking for.

    Thanks for the additional info about the deprecated code structures as well, I have never thought twice about it.


  •  

    Tags for this Thread

    Posting Permissions

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