Skip to content

Instantly share code, notes, and snippets.

@sssionggg
Created September 6, 2011 21:03
Show Gist options
  • Select an option

  • Save sssionggg/1198958 to your computer and use it in GitHub Desktop.

Select an option

Save sssionggg/1198958 to your computer and use it in GitHub Desktop.
gsub = (source, pattern, replacement) ->
unless pattern? and replacement?
return source
result = ''
while source.length > 0
if (match = source.match(pattern))
result += source.slice(0, match.index)
result += replacement
source = source.slice(match.index + match[0].length)
else
result += source
source = ''
result
gsub = function(source, pattern, replacement) {
var match, result;
if (!((pattern != null) && (replacement != null))) {
return source;
}
result = '';
while (source.length > 0) {
if ((match = source.match(pattern))) {
result += source.slice(0, match.index);
result += replacement;
source = source.slice(match.index + match[0].length);
} else {
result += source;
source = '';
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment