/ Published in: JavaScript

Based on Prototype JS String#gsub
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; }
Comments
