mercredi 14 mai 2014

loop through and .replace using regex


Vote count:

0




I have a list of url extensions that i want to string replace using regex so that both upper and lower case is captured like this:



str = str.replace(/\.net/i,"")
.replace(/\.com/i,"")
.replace(/\.org/i,"")
.replace(/\.net/i,"")
.replace(/\.int/i,"")
.replace(/\.edu/i,"")
.replace(/\.gov/i,"")
.replace(/\.mil/i,"")
.replace(/\.arpa/i,"")
.replace(/\.ac/i,"")
.replace(/\.ad/i,"")
.replace(/\.ae/i,"")
.replace(/\.af/i,"");


When i try to clean this up using arrays and loops like so, i get an error. Can anyone help me with syntax please



var arr = [ "net","com","org","net","int","edu","gov","mil","arpa","ac","ad","ae"];

str = str.replace(/\.com/i,"")
for(ii==0;ii<=arr.length;ii++){
.replace('/\.'+arr[ii]+'/i',"") // i know this '/\.' and '/i' should not be a string but how would i write it?
}
.replace(/\.af/i,"");


asked 36 secs ago

t q

1,317





Aucun commentaire:

Enregistrer un commentaire