JavaScript - matching non-ascii characters using regex

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

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

I expect ["@Agneš", "@Petar"] , but get ["@Agne", "@Petar"] . As you can see, the š symbol does not match.

How can I match all letter characters, including non-ascii?

+5
source share
2 answers

Until ES6 support for unicode in a regular expression is implemented, you can work around this with some type of:

 /@[^\s,]+/g 

where you simply list material that may not be in user names. Next year

 /@\w+/gu 

To make sure you don’t get half the email addresses and other cases where the @ value is in the middle of the word, it should be match(/[^\s,@]*@[^\s,@]+(?=[\s,]|$)/g) , and then filter results starting with "@".

+4
source

š is not a word symbol, and “w” is for a word symbol.

-1
source

All Articles