AS2 version here
Please see right hand pane before copying my code - thanks! >>
//needle and/or replacement can be either a string or an array of strings:
function replace ( needle, replacement, haystack:String):String{
var r=haystack;
if(typeof(needle)=="string"){
needle = [needle]
}
if(typeof(replacement)=="string"){
replacement = [replacement]
}
while(replacement.length<needle.length){
replacement.push(replacement[replacement.length-1])
}
for(var i in needle){
r = r.split(needle[i]).join(replacement[i])
}
return(r)
}
//SAMPLE USAGE:
trace( replace( "A", "X", "ABCDEFG" ) ) //outputs: XBCDEF
trace( replace(["A","B","C"] , ["X","Y","Z"], "ABCDEFG") ) //outputs: XYZDEF

No comments:
Post a Comment