Script snippet - removing duplicate terms in a list
Short snippet that will remove duplicate terms in a list. I futzed around with this for a while trying to figure out the proper regex and methods to accomplish this.
I start with a list of terms, copy the first to a new array, globally delete, copy the next, and keep going until I'm through all of them. Thought someone might find this useful.
try{
var term = ''; //new string
var terma = []; //new array
term = '444\n111\n333\n444a\n111a\n333\n111\n333a\n333\n222\n444'; //sample string
term = term.replace(/\n\n/g, '\n'); //strip blank lines
term = term.replace(/\n$/, ''); //strip trailing return
term = term.replace(/^\n/, ''); //strip leading return
terma = term.split('\n'); //place into array
var clean = []; //new array
var n = 0; //counter
while(terma.length > 0 && terma[0] != ''){ //iterate through all items
clean[n] = terma[0]; //copy first item
var regterm = terma[0]; //term to replace
var regex = new RegExp('^' + regterm + '$','gm'); //build regex
term = terma.join('\n'); //makew string
term = term.replace(regex, ''); //remove exact term
term = term.replace(/\n\n/g, '\n'); //strip blank lines
term = term.replace(/^\n/, ''); //strip leading return
terma = term.split('\n'); //place into array
n = n + 1; //count
}
alert(clean);
}
catch(e){
alert (e + e.line);
}
