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

Need a script to replace strings in a list. 1

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
Hi,

Can anyone do me a quick script which will check a comma delimited list for an occurance of a given word, and if it exists, remove it.

i.e. LIST = "value1,value2,value3,value1"

I need the script to check the list for ALL occurances of a word which is passed to it via a variable. So if the word which is passed to the script is "value1", it should then remove both the "value1" from the list, then return the edited list back as the same variable it was given.

Any help would be greatly appreciated.

Many Thanks,
Mark
 
Code:
function removeFromList(wordList,targetWord,delimiter){
 var wordArray = wordList.split(delimiter);
 wordList = '';
 for(x=0; x<wordArray.length; x++){
  if(wordArray[x] != targetWord){
   if(wordList != ''){
    wordList = wordList + ',' + wordArray[x];
   }
   else{
    wordList = wordArray[x];
   }
  }
 }
 return wordList;
}
 
function ReplaceDemo(){
var r, re; //Declare variables.
var ss = &quot;The man hit the ball with the bat.\n&quot;;
ss += &quot;while the fielder caught the ball with the glove.&quot;;
re = /The/g; //Create regular expression pattern.
r = ss.replace(re, &quot;A&quot;); //Replace &quot;A&quot; with &quot;The&quot;.
return(r); //Return string with replacement made.
}

straight from the SCripting help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top