extracting digits with regular expression

Discussion in 'JavaScript' started by bonecone, Jun 4, 2011.

  1. #1
    I have a string like this "d2-23-543-56". It could be of any length, have any number of hyphens and will always end with an integer. I'm trying to come up with a regular expression that will match everything up to and including the last hyphen so that I can replace it with an empty string and have only the 56 left over, or whatever integer happens to be at the end of the string.

    I've been fooling around with a regular expression tester for hours trying to figure this out, anybody?
     
    bonecone, Jun 4, 2011 IP
  2. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #2
    Hy,
    Maybe in this case you can use the split() method.
    Try this example:
    
    <script type="text/javascript"><!--
    // by MarPlo - www.coursesweb.net
    var num = 'd2-23-543-56';
    var ar_num = num.split('-');
    var len_num = ar_num.length;
    
    alert(ar_num[len_num-1]);         // 56
    --></script>
    
    Code (markup):
     
    MarPlo, Jun 4, 2011 IP