Skip to content

Instantly share code, notes, and snippets.

@robinduckett
Created June 2, 2010 14:04
Show Gist options
  • Save robinduckett/422404 to your computer and use it in GitHub Desktop.
Save robinduckett/422404 to your computer and use it in GitHub Desktop.
String.prototype.closest = function(find, pos) {
var found = -1;
var cur1,cur2; cur1 = cur2 = pos;
while (cur1 > 0 || cur2 <= this.length) {
is = this.substr(cur1, find.length);
it = this.substr(cur2, find.length);
if (is == find) return cur1;
if (it == find) return cur2;
if (cur1 > 0) cur1--;
if (cur2 <= this.length) cur2++;
}
return -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment