Return to Snippet

Revision: 47204
at June 2, 2011 15:23 by kendsnyder


Initial Code
function gsub(source, pattern, replacement) {
	var result = '', match, replaced;
	while (source.length > 0) {
		if (match = source.match(pattern)) {
			result += source.slice(0, match.index);
			replaced = replacement(match);
			result += replaced == null ? '' : String(replaced);
			source = source.slice(match.index + match[0].length);
		} else {
			result += source, source = '';
		}
	}
	return result;
}

Initial URL

                                

Initial Description
Based on Prototype JS String#gsub

Initial Title
JavaScript String gsub

Initial Tags
javascript

Initial Language
JavaScript