Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: do find/replace in a string from a list or text file


Message #1 by carl@o... on Thu, 7 Mar 2002 17:32:21
Sorry - this is the full version - the previous one posted too early!

Ideally I'd like to do a find/replace in a string from a text file, but 
failing that a list in the code would be acceptable:

Text/csv file contains:
red,blue
green,yellow
orange,purple

so the idea would be to use this as inpout and inspect a string replacing 
all occurrences of 'red' with 'blue', 'green' with 'yellow' and 'orange' 
with 'purple'.  I'd also like to be able to match case so that 'RED' 
would be replaced with 'BLUE' and 'Red' with 'Blue' rather than 'Red' 
with 'blue'.

ie:
text file version
(Pseudo Code)
Open mytextfile.txt
for each line in mytextfile
  do
    replace(mystring, line) (line contains 'red,blue')
  loop

or list in code version
var = new array("red,blue", "green, yellow", "orange, purple")
for i = 1, i<var.len, i++
        replace(mystring, var) (var contains 'red,blue')
loop

I've looked far and wide for 2 days now and cannot get this right.  
Please help me before I go mad!

Thanks
  

  Return to Index