Skip to content

Instantly share code, notes, and snippets.

@daxxog
Created March 1, 2013 17:57
Show Gist options
  • Save daxxog/5066476 to your computer and use it in GitHub Desktop.
Save daxxog/5066476 to your computer and use it in GitHub Desktop.
.replace without regex
String.prototype.replaceOne = function(_what, _with) {
var what = _what.split(''),
that = this.split(''),
found = false,
skip = 0,
_new = [];
that.forEach(function(v, i, a) {
var fail = false;
if(skip > 0) {
skip--;
} else {
if((found === false) && (v === what[0])) {
skip = -1;
for(var k = 0; k<what.length; k++) {
skip++;
if(that[i + k] !== what[k]) {
fail = true;
break;
}
}
if(fail === false) {
found = true;
_new.push(_with);
} else {
skip = 0;
_new.push(v);
}
} else {
_new.push(v);
}
}
});
return _new.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment