Regular expression for twitter username

前端 未结 9 2464
挽巷
挽巷 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

    This is the best solution I found yet to replace multiple occurrences of a twitter username.

    The regex doing the trick is /(^|[^@\w])@(\w{1,15})\b/. I am catching what stand behind the @ character so I can replace the username correctly. And I am using global match flag (g) so it will replace all occurrences. asenovm answer is simple, but will not work in most user input contexts, as techexpert is explaining in his comment.

    var output,
        text    = "@RayFranco is answering to @AnPel, this is a real '@username83' but this is an@email.com, and this is a @probablyfaketwitterusername",
        regex   = /(^|[^@\w])@(\w{1,15})\b/g,
        replace = '$1<a href="http://twitter.com/$2">@$2</a>';
    
    output = text.replace( regex, replace );
    
    console.log ( output );
    

    This is giving me what I expected (tested with node v0.9.1) :

    @RayFranco is answering to @AnPel, this is a real '@username83' but this is an@email.com, and this is a @probablyfaketwitterusername

    This is based on Twitter "specs" for username :

    Your username cannot be longer than 15 characters. Your real name can be longer (20 characters), but usernames are kept shorter for the sake of ease. A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) with the exception of underscores, as noted above. Check to make sure your desired username doesn't contain any symbols, dashes, or spaces.

    Hope this helps.

    0 讨论(0)
  • 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, "<a href='$1' target='_blank'>$1</a>");
        exp = /(^|\s)#(\w+)/g;
        text = text.replace(exp, "$1<a href='https://twitter.com/hashtag/$2?src=hash' target='_blank'>#$2</a>");
        exp = /(^|\s)@(\w+)/g;
        text = text.replace(exp, "$1<a href='http://www.twitter.com/$2' target='_blank'>@$2</a>");
        return text;
    }
    
    0 讨论(0)
  • 2020-12-15 04:18

    A short an easy way to do it:

    function validTwitteUser(sn) {
        return /^[a-zA-Z0-9_]{1,15}$/.test(sn);
    }
    
    0 讨论(0)
  • 2020-12-15 04:19

    Another one that does the work is:

    /(?<!\w)@[\w+]{1,15}\b /

    • The first part (?<!\w)@ matches @ if there is no character value before it. That can be used as a filter for potential email addresses that have the format text@mail.com. Since there is text before @ the email address is not matched.

    • The second part [\w+]{1,15}\b matches a string of word characters namely a-z,A-Z,0-9,_ that are accepted in a twitter username. The word boundary anchor\b matches the space between a word character and a non word character. Non word characters are not accepted in the twitter username and are therefore excluded from the match because of \b.

    Last but not least I provide a picture where I tested the proposed regex on the text provided in the second most voted answer by rayfranco to check for matches.

    screenshot, screenshot image

    0 讨论(0)
  • 2020-12-15 04:21

    This should do: ^@?(\w){1,15}$

    0 讨论(0)
  • 2020-12-15 04:21

    This could be help for a Full Match :

    ^@(?=.*\w)[\w]{1,15}$
    

    Testing: @John_Doe2000

    Results:

    Match 1
    Full match  0-13    @John_Doe2000
    

    Try Here

    0 讨论(0)
提交回复
热议问题