简体   繁体   English

JavaScript - 使用正则表达式匹配非ascii符号

[英]JavaScript - match non-ascii symbols using regex

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"] . 我期待["@Agneš", "@Petar"]但是得到了["@Agne", "@Petar"] As you can see š symbol is not matched. 如您所见, š符号不匹配。

How can I match all letter symbols include non-ascii? 如何匹配所有字母符号包括非ascii?

Until ES6 support for unicode in regex is implemented, you can work around it with somehting like: 在实现ES6对正则表达式中unicode的支持之前,您可以使用以下方法来解决它:

/@[^\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 "@". 一种方法可以确保你没有得到一半的电子邮件地址和@在一个单词中间的其他情况match(/[^\\s,@]*@[^\\s,@]+(?=[\\s,]|$)/g)然后filter结果是否以“@”开头。

š不是单词字符,“w”代表单词字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM