Skip to content

Instantly share code, notes, and snippets.

@camerond
Created June 13, 2012 20:18
Show Gist options
  • Save camerond/2926259 to your computer and use it in GitHub Desktop.
Save camerond/2926259 to your computer and use it in GitHub Desktop.
Get the location of an @reply symbol in a string
# Implementation for a plugin that allows vaguely-Tweetbot-style @mention autofills.
# Finds the index of the @ symbol that should trigger a mention: the @ closest to the typing cursor
# that is preceded by a non-word character or the beginning of the string
#
# "@dave hey"
# ^
#
# "@john, I thought you should talk to @tony"
# ^
#
# "john@internet.com is his email address (@dave should know it)
# ^
$textarea
search_in = $textarea.val().slice(0, $textarea[0].selectionStart)
regex = /(^|[\W])@/gi
while (sym = regex.exec(search_in))
at_symbol = if sym.index == 0 then sym.index else sym.index + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment