homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Javscript parsing
extracting specific substrings from a string

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

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)

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

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)

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)

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)'

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved