Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse string for numbers of units 1

Status
Not open for further replies.

nuttyernurse

Technical User
May 18, 2009
35
US
I need to split the string below to only get the numbers after the "=" sign and before the word "unit" There is a possibility that this could be a 2 digit number. I will need to have each number available to assign to a text box.

Sensitive 0601-2059 bg</=70 hypoglycemia*71-150 no insulin*151-200=1 unit*201-250=2 unit*251-300=3 unit*301-350=4 unit*351-400=5 unit* 2100-0600 bg</=70 hypoglycemia*71-200 no insulin*201-250=0 unit*251-300=1 unit*301-399=2 unit*

I was able to get help with getting the numbers to display an alert, but I need the numbers to be separate values I can work with.

Here is the script with the alert:

function splitString(){
var str="Sensitive 0601-2059 bg</=70 hypoglycemia*71-150 no insulin*151-200=1 unit*201-250=2 unit*251-300=3 unit*301-350=4 unit*351-400=5 unit* 2100-0600 bg</=70 hypoglycemia*71-200 no insulin*201-250=0 unit*251-300=1 unit*301-399=2 unit*";
var wordArray = str.split("=");

for(i = 0;i<wordArray.length;i++){
//Split now on unit
if(wordArray.indexOf("unit")>0){
//Split on unit
var wordArray2 = wordArray.split("unit");
alert("Number is "+wordArray2[0]);
}
}

}


Thank you for any help you can offer!
 
Hi

What do you need exactly ? I do not really understand the part with the "text box". As you have the numbers, where is the problem in using them ?

By the way, just to extract those numbers a [tt]match()[/tt] would be almost[sup](*)[/sup] enough. This returns an array with all found unit numbers :
JavaScript:
[gray]// supposing str was previously declared as in your original code[/gray]
str[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/=\d+(?=\s*unit)/g[/fuchsia][teal]).[/teal][COLOR=darkgoldenrod]map[/color][teal]([/teal][b]function[/b][teal]([/teal]one[teal])[/teal][teal]{[/teal][b]return[/b] one[teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal])[/teal][teal]}[/teal][teal])[/teal]
[small](*) Sadly, JavaScript regular expressions not support zero-width positive look-behind assertion, so the equal sign must be removed "manually" from the matches.[/small]


Feherke.
 
What about using a word boundary? That way, the map call can be removed:

Code:
var str = 'Sensitive 0601-2059 bg</=70 hypoglycemia*71-150 no insulin*151-200=1 unit*201-250=2 unit*251-300=3 unit*301-350=4 unit*351-400=5 unit* 2100-0600 bg</=70 hypoglycemia*71-200 no insulin*201-250=0 unit*251-300=1 unit*301-399=2 unit*';

str.match(/\b\d+(?= unit)/g); // ["1", "2", "3", "4", "5", "0", "1", "2"]

Hope this helps,

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[blue]@[/blue] Code Couch:
[blue]@[/blue] Twitter:
 
Hi

Good point. Depends on what the OP would like to extract from something like [tt]str[teal]=[/teal][green]'formula:1 unit+2 unit=3 unit'[/green][/tt].


Feherke.
 

Your solution is better if there is a possibility of a number appearing before the word unit, e.g.:

Code:
var str = 'Sensitive 0601-2059 bg</=70 hypoglycemia [!]5 unit[/!]*71-150 no insulin*151-200=1 unit*201-250=2 unit*251-300=3 unit*301-350=4 unit*351-400=5 unit* 2100-0600 bg</=70 hypoglycemia*71-200 no insulin*201-250=0 unit*251-300=1 unit*301-399=2 unit*';

In this scenario, my code would erroneously pick up the "5", where yours works as expected.

Dan


 
I'm sorry confusion, what I need is to extract just the number after the equal sign and before the space before the word unit. The numbers present in the string are just an example, but in reality could be a double digit number such as 15. This string is built outside of the HTML by another system, and I call it into the HTML. I need to pull it in, parse it, and put the resulting values into its individual text box so the end user could change the values. They would then submit the form and it would sent the modifications.

I have attache a link to the HTML file, there is a section called individualized on the dosage adjustment tab, I have colored yellow, that the results would go into. The boxes correlate with the 70-150 range, 151-200 range, etc.
 
 http://wikisend.com/download/961870/testing_ssi.htm
Hi

Something like this ?
JavaScript:
[b]var[/b] str[teal]=[/teal][green][i]"... as earlier ..."[/i][/green][teal];[/teal]
[b]var[/b] unit[teal]=[/teal]str[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/=\d+(?=\s*unit)/g[/fuchsia][teal])[/teal]
[b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]unit[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
  [b]var[/b] elem[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'Units'[/i][/green][teal]+([/teal]i[teal]+[/teal][purple]1[/purple][teal]))[/teal]
  [b]if[/b] [teal]([/teal]elem[teal])[/teal] elem[teal].[/teal]value[teal]=[/teal]unit[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal])[/teal]
[teal]}[/teal]
I still not understand the relation between the ##input#s and the matches, I just assigned them in order.


Feherke.
 
Thanks feherke, but I am unsure how to get the value into the text box, I'm still kind of a new, but not a neophyte.
 
Thanks for finding that! I changed it, still nothing populates.
 
Yup, sorry, originally had it go onload, was stuck in my mind.
 
feherke,

I found the problem, I had the same id's on tab one and tab two text boxes in the HTML. I change them and the JS and now it works!

THANK YOU!!!!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top