Question:
Is it possible to Search and Replace Array Values?
topgun77
2007-02-12 15:08:50 UTC
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.
Three answers:
Kryzchek
2007-02-12 15:19:52 UTC
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

}
Atif Majid
2007-02-13 13:04:25 UTC
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
wigginsray
2007-02-12 23:18:58 UTC
You need to investigate the javascript "Split" method.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...