Skip to content

Instantly share code, notes, and snippets.

@yukihr
Created October 30, 2011 13:04
Show Gist options
  • Save yukihr/1325883 to your computer and use it in GitHub Desktop.
Save yukihr/1325883 to your computer and use it in GitHub Desktop.
get (alphabet) word at point
function wordAtPoint(text, point) {
var wordRx = /[a-zA-Z_]+/, separatorRx = /(\s)/,
words, word, i, len, n;
words = text.split(separatorRx);
for (i = n = 0, len = words.length; i < len; i++) {
if (point < (n += words[i].length)) {
if (wordRx.test(words[i])) {
word = words[i];
}
break;
}
}
return word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment