View Full Version : Determine length of string? (extract number)

07-23-2010, 07:46 AM
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:

var start = [...predetermined...]
var end = 0;
while (!isNaN(sourceCode.charAt(start + end))){
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?

07-23-2010, 11:16 AM
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.

07-23-2010, 05:08 PM
The input string is actually html code. Something like:
<span id="somereallylongid">50</span>

Philip M
07-23-2010, 05:12 PM
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.

07-23-2010, 10:10 PM
Perfect! Thank you very much. Looks like I should spend some time learning regex :rolleyes:

Old Pedant
07-23-2010, 10:31 PM
FWIW, that regex of Philip's means this:

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/>

07-24-2010, 03:19 AM
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.

Old Pedant
07-24-2010, 03:36 AM
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:

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:

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" );

How's that?

07-24-2010, 03:51 AM
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?

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" );

Old Pedant
07-24-2010, 10:08 AM
Yes, but a tiny bit more work:

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" );

Untested, but I think it's right.