Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javscript parsing

extracting specific substrings from a string



8:30 pm on Jun 14, 2003 (gmt 0)

10+ Year Member

How can I make Javascript parse through a string until it finds a certain charecter, then store everything between that charecter and another symbol as a variable? Then it needs to keep going and repeating the process until teh end of the string.


1:48 am on Jun 15, 2003 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The key would be using the string.substring() method, which allows you to state beginning and ending character positions -- rather than using string.substr() which requires beginning position and length.

I'm not going to write the whole script for you, but here's how I see the logic for it:

1. assign a variable name to the string - s
2. initialize a loop counter variable - i
3. set up an empty array to hold your extracted variables
----- BEGIN LOOP -----
4. find position of the first "special" character - m
5. ESCAPE LOOP if "special" character is not found
6. find position of the "stop" character - n
7. ESCAPE LOOP if "stop" character is not found
8. define a new variable to be s.substring(m+1,n-1) - v
9. assign "v" to be a new array element (use the loop counter)
10. reassign the value of "s" to be s.substring(n+1,s.length)
11. increment the loop counter
----- LOOP BACK -----

That way you would create an array where each element was one of the variables you wanted to extract from the string.


2:21 am on Jun 15, 2003 (gmt 0)

10+ Year Member

Thanks a lot Tedster, that's perfect. I'm still learning Javascript, so I still don't have a handle on all the various techniques. This'll give me something to work on.


2:47 am on Jun 15, 2003 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I'm still learning Javascript

Aren't we all ;)

I've never had the luxury of a solid course of study, just the pressure of immediate needs, handled through Google searches and an O'Reilly book or two. We've got members here who know a lot more than I do, and who use javascript a lot more than I do.

Glad the logic helps out.


4:46 am on Jun 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

An alternative is to make use of some of the build-in functions. Advantages of using built-ins where possible are: you write less code hence less room for bugs, less to download, and it runs faster as the built-ins are compiled in the browser, not interpreted. Javascript has the 'indexOf()' built-in function.

var my_string = "hello this is a string";
var first = my_string.indexOf("o");
// Now "first" is set to 4, the position of the first "o" in the string.
// If it did not find the "o", it would return -1.

Then form a substring of my_string from first to my_string.length, and use indexOf() on the new string to find the occurance of the next character you are looking for. Then use substring to extract.


[added:] OOPS, I just realised this is not really an 'alternative', just more detail on how to do the steps 4 and 6 in Ted's post. However, another built-in that may be useful is:
string_name.split(separator char, limit)'


Featured Threads

Hot Threads This Week

Hot Threads This Month