English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I can't seem to find anything supporting this theory. In Javascript can I run a regular expression search and replace on an entire array?

For example:
Myarray[0] = "this is fun | what time is it? | the sky is blue|";
Myarray[1] = "Yahoo is fun | Answers are good | Go Saints |";
piper = /\|/g;
new_line = "\n";
Myarray[0] = Myarray[0] .replace(piper, new_line);

I want to search and replace all pipes with a newline character "\n". I then want all the arrays to read like this.

Myarray[0] = "this is fun
what time is it?
the sky is blue";
Myarray[1] = "Yahoo is fun
Answers are good
Go Saints ";

I keep getting an Object doesn't support this property or method. I have spent a good deal of time searching for a site that shows Array Search and Replace but to no avail.

2007-02-12 10:08:50 · 3 answers · asked by topgun77 2 in Computers & Internet Programming & Design

3 answers

All you need to do is iterate your array:

for (i = 0; i < Myarray.length; i++)
{
//Do your replacement here, just using "i" as the array index
Myarray[i]. //whatever
}

2007-02-12 10:19:52 · answer #1 · answered by Kryzchek 4 · 0 0

Hi

Regular expression works on a sigle string and not on arrays. So you will have to loop through array and replace pipes with \n using regular expression.
Regards

2007-02-13 08:04:25 · answer #2 · answered by Atif Majid 3 · 0 0

You need to investigate the javascript "Split" method.

2007-02-12 18:18:58 · answer #3 · answered by wigginsray 7 · 0 0

fedest.com, questions and answers