Regular expression for twitter username

前端 未结 9 2525
挽巷
挽巷 2020-12-15 03:43

I need a javascript regular expression to match twitter usernames.

The username is entered by the user while signing up, so I don\'t want to distract them with too m

9条回答
  •  孤街浪徒
    2020-12-15 04:17

    It may be more than you need but I found this in another post "regex how to replace twitter links". Wraps @usernames, #hashtags and urls. Working well for me.

    function processTweetLinks(text) {
        var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
        text = text.replace(exp, "$1");
        exp = /(^|\s)#(\w+)/g;
        text = text.replace(exp, "$1#$2");
        exp = /(^|\s)@(\w+)/g;
        text = text.replace(exp, "$1@$2");
        return text;
    }
    

提交回复
热议问题