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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove the last 16 digits at the end of text

    I have a string that looks like this:
    Code:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834
    I want to remove the digits at the end of the string, basically the 16 digits at the end of the string. In the end it should look like this:
    Code:
    [APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)
    This is my code that I have written so far
    Code:
    var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
    var n=str.substr(1,74);
    document.write(n);
    The problem is the string will be different so each will have different amount of characters. So how I remove the digits at the end of the string in javascript?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You could use a regular expression
    Code:
    var n = str.match(/(.*)_\d{8}_\d{6}$/)[1];

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Three other ways:-

    Code:
    <script type = "text/javascript">
    
    var str="[APPLE PIE] Sei Shoujo Sentai Lakers 3 Battle Team Lakers 3 (100% FULL-PIC)_20121104_032834";
    var str1 = str.replace(/(\)).*/i, "$1");  // assumes final character before digits is ) 
    alert (str1);
    
    var len = str.length;
    var str2 = str.substring(0,len-16);  // assumes digits length is always 16
    alert (str2);
    
    var str3 = str.replace(/[0-9\_]+$/gi, "");
    alert (str3);
    
    </script>
    The third one is probably the best.
    Last edited by Philip M; 11-26-2012 at 11:07 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    That last option can be made two characters shorter as \d is the same as 0-9 and _ doesn't need to be escaped in that spot.

    Code:
    var str3 = str.replace(/[\d_]+$/gi, "");
    alert (str3);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by felgall View Post
    That last option can be made two characters shorter as \d is the same as 0-9 and _ doesn't need to be escaped in that spot.

    Code:
    var str3 = str.replace(/[\d_]+$/gi, "");
    alert (str3);
    Big deal! What, 2 whole characters? That could make a big difference to the speed of execution.
    I always escape special characters for clarity.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by felgall View Post
    That last option can be made two characters shorter as \d is the same as 0-9 and _ doesn't need to be escaped in that spot.

    Code:
    var str3 = str.replace(/[\d_]+$/gi, "");
    alert (str3);
    I'm surprised you didn't spot that the g & i flags are redundant.


  •  

    Posting Permissions

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