5

I want to match all mentioned users in comment. Example:

var comment = '@Agneš, @Petar, please take a look at this';
var mentionedUsers = comment.match(/@\w+/g);

console.log(mentionedUsers)

I'm expecting ["@Agneš", "@Petar"] but getting ["@Agne", "@Petar"]. As you can see š symbol is not matched.

How can I match all letter symbols include non-ascii?

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • 1
    Make sure special characters are converted to their regular counterpart as they are they typed in the comment field, and you won't have this issue. – adeneo Apr 20 '15 at 15:01
  • 1
    "special characters"? what world do you live in? they're perfectly normal Unicode letters. It's a JavaScript problem (for a little while longer). – Touffy Apr 20 '15 at 15:02
  • Special character, as in anything that isn't `a-z`. Just converting the names in the comment field, the way the site you're on right now does, is by far the easiest. – adeneo Apr 20 '15 at 15:06
  • You need to use `XRegExp` library. – Avinash Raj Apr 20 '15 at 15:06
  • You have to ensure that your JS source file has Unicode (UTF-8) charset. Otherwise your Regexp won't work as you expected – hindmost Apr 20 '15 at 15:16

2 Answers2

4

Until ES6 support for unicode in regex is implemented, you can work around it with somehting like:

/@[^\s,]+/g

where you just list stuff that can't be in usernames. Next year,

/@\w+/gu

A way to make sure you don't get halves of email adresses and other cases where the @ is in the middle of a word would be to match(/[^\s,@]*@[^\s,@]+(?=[\s,]|$)/g) and then filter the results on whether they start with "@".

Touffy
  • 6,309
  • 22
  • 28
  • 2
    Although it's far from perfect. Will match parts of email adresses, and without lookbehind (which isn't supported in JavaScript either), there's not much we can do about it. – Touffy Apr 20 '15 at 15:09
-1

the š is not a word character, and "w" is for word character.

Pechou
  • 85
  • 8