javascript 如何在javascript中从像twitter这样的字符串中提取@提及
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15265605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pull @ mentions out of strings like twitter in javascript
提问by jpotts18
I am writing an application in Node.js that allows users to mention each other in messages like on twitter. I want to be able to find the user and send them a notification. In order to do this I need to pull @usernames to find mentions from a string in node.js?
我正在用 Node.js 编写一个应用程序,它允许用户在 Twitter 等消息中相互提及。我希望能够找到用户并向他们发送通知。为了做到这一点,我需要拉 @usernames 以从 node.js 中的字符串中查找提及?
Any advice, regex, problems?
任何建议,正则表达式,问题?
回答by jpotts18
I have found that this is the best way to find mentions inside of a string in javascript.
我发现这是在 javascript 中的字符串中查找提及的最佳方法。
var str = "@jpotts18 what is up man? Are you hanging out with @kyle_clegg";
var pattern = /\B@[a-z0-9_-]+/gi;
str.match(pattern);
["@jpotts18", "@kyle_clegg"]
I have purposefully restricted it to upper and lowercase alpha numeric and (-,_) symbols in order to avoid periods that could be confused for usernames like (@j.potts).
我有意将其限制为大写和小写字母数字和 (-,_) 符号,以避免可能与 (@j.potts) 等用户名混淆的句点。
This is what twitter-text.jsis doing behind the scenes.
这就是twitter-text.js在幕后所做的。
// Mention related regex collection
twttr.txt.regexen.validMentionPrecedingChars = /(?:^|[^a-zA-Z0-9_!#$%&*@@]|RT:?)/;
twttr.txt.regexen.atSigns = /[@@]/;
twttr.txt.regexen.validMentionOrList = regexSupplant(
'(#{validMentionPrecedingChars})' + // : Preceding character
'(#{atSigns})' + // : At mark
'([a-zA-Z0-9_]{1,20})' + // : Screen name
'(\/[a-zA-Z][a-zA-Z0-9_\-]{0,24})?' // : List (optional)
, 'g');
twttr.txt.regexen.endMentionMatch = regexSupplant(/^(?:#{atSigns}|[#{latinAccentChars}]|:\/\/)/);
Please let me know if you have used anything that is more efficient, or accurate. Thanks!
如果您使用了更有效或更准确的方法,请告诉我。谢谢!
回答by Nick Mitchinson
Twitter has a library that you should be able to use for this. https://github.com/twitter/twitter-text-js.
Twitter 有一个库,您应该可以使用它。https://github.com/twitter/twitter-text-js。
I haven't used it, but if you trust its description, "the library provides autolinking and extraction for URLs, usernames, lists, and hashtags.". You should be able to use it in Node with npm install twitter-text
.
我没有使用过它,但如果你相信它的描述,“该库提供了 URL、用户名、列表和主题标签的自动链接和提取。”。您应该可以在 Node 中使用它npm install twitter-text
。
While I understand that you're not looking for Twitter usernames, the same logic still applies and you should be able to use it fine (it does not validate that extracted usernames are valid twitter usernames). If not, forking it for your own purposes may be a very good place to start.
虽然我知道您不是在寻找 Twitter 用户名,但相同的逻辑仍然适用,您应该可以很好地使用它(它不会验证提取的用户名是否是有效的 Twitter 用户名)。如果没有,为自己的目的分叉它可能是一个很好的起点。
Edit: I looked at the docs closer, and there is a perfect example of what you need right here.
编辑:我看了看文档近了,有你需要正确的东西一个很好的例子在这里。
var usernames = twttr.txt.extractMentions("Mentioning @twitter and @Hyman")
// usernames == ["twitter", "Hyman"]
回答by nacholibre
here is how you extract mentions from instagram caption with JavaScript and underscore.
这是使用 JavaScript 和下划线从 Instagram 标题中提取提及的方法。
var _ = require('underscore');
function parseMentions(text) {
var mentionsRegex = new RegExp('@([a-zA-Z0-9\_\.]+)', 'gim');
var matches = text.match(mentionsRegex);
if (matches && matches.length) {
matches = matches.map(function(match) {
return match.slice(1);
});
return _.uniq(matches);
} else {
return [];
}
}