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?

5
  • 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. Commented Apr 20, 2015 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). Commented Apr 20, 2015 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. Commented Apr 20, 2015 at 15:06
  • You need to use XRegExp library. Commented Apr 20, 2015 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 Commented Apr 20, 2015 at 15:16

2 Answers 2

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 "@".

Sign up to request clarification or add additional context in comments.

1 Comment

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.
-1

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

1 Comment

In PHP, Python š is just normal utf letter. Can't believe JS still have no full utf support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.