Skip to content

Instantly share code, notes, and snippets.

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