how to pull @ mentions out of strings like twitter in javascript

前端 未结 3 1807
野性不改
野性不改 2020-12-04 16:54

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.

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 17:27

    here is how you extract mentions from instagram caption with JavaScript and underscore.

    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 [];
        }
    }
    

提交回复
热议问题