leodutra
10/28/2013 - 5:41 PM

Replace multiple times a string or array using multiple patterns.

Replace multiple times a string or array using multiple patterns.

/**
 * WARNING: some browsers do not respect property definition order.
 * DO NOT try to do cross dependent replacement (ex: replace "2" using data replaced by replace "1").
 */
 
function multiplace(str, def/* {replacement: regexp} */) {
	str = '' + ($.type(str) == 'array' ? str.join('') : str);
	for (var replacement in def) {
		if (def.hasOwnProperty(replacement)) {
			str = str.replace(def[replacement], replacement);
		}
	}
	return str;
}