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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Determine length of string? (extract number)

    I have a string of text, and starting from a particular spot (known to be a number), I want to find the length of that number.

    Here's what I have:
    Code:
    ...
    
    var start = [...predetermined...]
    var end = 0;
    while (!isNaN(sourceCode.charAt(start + end))){
       endCurrent++;
    }
    var myNum = sourceCode.substr(start, end) * 1;
    So let's say var sourceCode = "alkdjabjasdsdf-53 dnalsdb..."; , startCurrent will already be at the "5" and I want to be able to extract the "53". What I have works, but it seems cumbersome...

    Any advice?

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    What's the structure of this input string? Does it follow any pattern? Sounds like what you're trying to do may well be easily accomplished with a regex, but it depends on what the input is and what you want to do with it.

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    The input string is actually html code. Something like:
    Code:
    <span id="somereallylongid">50</span>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    var sourcecode = "alkdjabjasdsdf-53 dnalsdb";
    var x = sourcecode.replace(/[^0-9]/gi,"");
    alert (x);
    alert (x.length);
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Perfect! Thank you very much. Looks like I should spend some time learning regex

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    FWIW, that regex of Philip's means this:

    Code:
    var x = sourcecode.replace(/[^0-9]/gi,"");
    
    /.../ ==>> denotes the limits of the expression
    [...] ==>> denotes any one of a group of characters
    [^...] ==>> inverts the sense of "group of characters"
    0-9 ==>> the range of characters from '0' to '9' ... in other words, all the digits
    [^0-9] ==>> "any character *NOT* in the group from '0' to '9'
    i ==>> ignore case (really irrelevant to this regexp, because upper/lower case irrelevant here)
    g ==>> "global", change *ALL* such patterns.
    
    replace( anyregexp, "" ) ==>> replace whatever matches the regexp with blank string
    
    replace(/[^0-9]/ig, "" ) ==>> replace all characters *EXCEPT* '0' through '9' with blank string.
    
    *********
    
    Could have also written that more succinctly thus:
    
    replace( /\D/g, "" ) ==>> \D means "all characters except digits" (exact equivalent of [^0-9])
    Hi, Philip! Just couldn't stay away, huh?? <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    qwertyuiop (07-24-2010)

  • #7
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Thanks for that informative explanation Old Pendant!

    Final question, while this works really well and works for my purposes, I'm just wondering if regex can also extract the number until it reaches a non-numeric character. My bloated while loop did this.

    For example, we have a string like this: "<span id="blahblahblah">500</span>". It's known that the number will never be more than 3 digits, so I can a short substring and then apply your guy's code, how would I set up a reg expression that will parse a number until it reaches a non-number (which would be the "<")? Idealy, it extracts the number between the span tag?

    I came up with this: /[0-9]{1,3}/ (find number with either 1, 2, or 3 digits if I understood correctly). How do I only match the first occurance? It seem this will match all in the string.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    No, if you don't use /g on the end of the regex, then it only finds the *first* occurrence.

    But finding it and using it are two different things.

    Incidentally, you could have done:
    Code:
    /\<span[^\>]*\>\d+\</
    to ensure you got the value in the <span>nnn</span>.

    Anyway, using the value is harder. You could use match() and then take apart the return value from match(), but another way might be this:
    Code:
    <script>
    var str = "now is the <b>best</b> time to find <span id='zam'>347</span> dollars";
    
    var findit = /^.*?\<span[^\>]*\>(\d+)\<.*$/i ;
    
    var digits = str.replace( findit, "$1" );
    alert(digits);
    </script>
    How's that?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Cool, now I'm trying to take it one step further, throwing in the id="" into the span. So if i know the ID I want, is there a way to put a variable into the regex?

    Code:
    var str = "now is the <b>best</b> time to find <span id='zam'>347</span> dollars";
    var spanID = "zam";
    var findit = /^.*?\<span id=\'/+spanID+/\'[^\>]*\>(\d+)\<.*$/i ; //doesn't work
    
    var digits = str.replace( findit, "$1" );
    alert(digits);

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Yes, but a tiny bit more work:

    Code:
    var str = "now is the <b>best</b> time to find <span id='zam'>347</span> dollars";
    var spanID = "zam";
    var findit = new RegExp("^.*?\\<span id=\\'"+spanID+"\\'[^\\>]*\\>(\\d+)\\<.*$","i");
    
    var digits = str.replace( findit, "$1" );
    alert(digits);
    Untested, but I think it's right.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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