Besides getting all matches and getting the last, you can use capture groups to get the last match:
var word=/.*(@\w+)/i; var name = content.match(word)[1];
Or using exec, everything will look like this:
var word=/.*(@\w+)/i; $("#comment").on("input",function() {
Fiddle
PS, if your goal is a more general approach, and you need to switch between getting all the words and one, I would recommend keeping the global correspondence and getting the last, as in Jonas answer.
source share