You need to iterate the array f and try each replacement separately.
jQuery('#colCenterAddress').val(function(i,val) { var f = ['Rd','St','Ave']; var r = ['Road','Street','Avenue']; $.each(f,function(i,v) { val = val.replace(new RegExp('\\b' + v + '\\b', 'g'),r[i]); }); return val; });
DEMO: http://jsfiddle.net/vRTNt/
If this is what you are going to do on a regular basis, you might want to save arrays and even create a third array with pre-created regular expressions.
var f = ['Rd','St','Ave']; var r = ['Road','Street','Avenue']; var re = $.map(f, function(v,i) { return new RegExp('\\b' + v + '\\b', 'g'); }); jQuery('#colCenterAddress').val(function(i,val) { $.each(f,function(i,v) { val = val.replace(re[i],r[i]); }); return val; });
DEMO: http://jsfiddle.net/vRTNt/1/
user1106925
source share