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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse a string into an array

Status
Not open for further replies.

fishiface

IS-IT--Management
Feb 24, 2003
687
GB
I've seen but can't find or recall a post in this thread where someone parsed a string into an array using a regular expression to define the delimiter. I need to do something similar and would be grateful for an example or a pointer to a decent reference resource.

In perl-speak, I would
[tt] my @array = split( /regex/, $string );[/tt]

Yours,

"As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
Code:
var myArray = "help,me,parse,this,string,please".split(",");

does that help?
-pete
 
It certainly does, sir, and many thanks to you. Can you point me towards a site where I can learn more of this sort of information? It does not seem to be in the core language specification and yet I cannot find it in the DOM. I feel certain that there is something somewhere that I am missing! "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
>> Can you point me towards a site ...

At the bottom of the left column of this web site you will find a "Partners" box. It is full of places to go to learn more.

good luck
-pete
 
Great. That will keep me busy/quiet for a while. Thanks again. "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
fishiface,

the js split() function accepts regex, as does replace():
Code:
var str = "i_am-a-string^of*chars";
alert(str.split(/[-_]/));
alert(str.replace(/[*^]/gi,"!"));
=========================================================
while (!succeed) try();
-jeff
 
use string.indexOf function to find the delimiter. This will save you a lot of grief.
 
maxpower1,

regexes will allow multiple delimiters within one statement, and will allow more complex patterns than the static text that indexOf() accepts...you cannot search for "any letter followed by two numbers and a hyphen" using indexOf().

=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top