PDA

View Full Version : extracting part of string


snowball
06-28-2002, 02:37 PM
Hi

Can anyone help please ... I am only a novice at javascript.

I have a field called "index_name[counter].url" which is very long it has 30-40 characters ........ although it is always in the same format ........

http://www.mysite.com/action.pl?n33253396&f=409790

What code would I have to use to extract "33253396" from the rest of the code and call it something like "num" ....

Any help would be appreciated .... but please be kind and assume I know nothing! The more precise you can be the better.

Many thanks to anyone who can help.

snow

x_goose_x
06-28-2002, 04:11 PM
url = location.search;
num = url.substring(2,url.indexOf("&"));

bcarl314
06-28-2002, 05:05 PM
If you are dealing with different URLS, using regular expressions would be a better way to go. Just food for thought.

snowball
06-28-2002, 07:43 PM
Sorry Guys ....

This doesn't work .... or at least I don't have the knowledge to make it work ..........

I have a field which when I print it into a page is called:

+"index_name[counter].url"+

Now instead of printing that I want to print a field called:

+"num"+



So to do that, before the print routine I type:

num = index_name[counter].url;



But that prints all of it, namely:

http://www.mysite.com/action.pl?n33253396&f=409790



All I want is ...........33253396

So what do I put after or instead of (please state exactly which) the line:

num = index_name[counter].url;

to make it happen .............



Sorry if I am being stupid ..... but I am new to this and struggling a bit ..... any help would be appreciated!

Thanks

snow

joh6nn
06-28-2002, 07:53 PM
num = index_name[counter].url;
num = num.slice(num.indexOf('?')+1,num.indexOf('&'));

that'll give you a string representation of the number. if you want it to actually be a number, so that you can add or subtract from it, then you have to add one more line:

num = Number(num);

hope that does what you want.

snowball
06-29-2002, 03:36 PM
Thanks ... that's excellent. It works well

snow


:)