Javascript 推特用户名的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8650007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:55:13  来源:igfitidea点击:

Regular expression for twitter username

javascriptregex

提问by ppp

I need a javascript regular expression to match twitter usernames.

我需要一个 javascript 正则表达式来匹配 twitter 用户名。

The username is entered by the user while signing up, so I don't want to distract them with too many error notifications. Because of that, I need the expression to match valid usernames regardles if they have the @ before the username or not.

用户名是用户在注册时输入的,所以我不想让太多的错误通知分散他们的注意力。因此,我需要表达式来匹配有效的用户名,不管他们在用户名之前是否有@。

Twitter usernames can contain latin characters, underscores and numbers, and the only limitation is the can be up to 15 characters long. ( but I need the regex to match 16 characters as well, in case someone enters the @ before the username ).

Twitter 用户名可以包含拉丁字符、下划线和数字,唯一的限制是最长可达 15 个字符。(但我还需要正则表达式来匹配 16 个字符,以防有人在用户名之前输入 @ )。

回答by asenovm

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

这应该做: ^@?(\w){1,15}$

回答by rayfranco

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

这是我发现的最好的解决方案,可以替换多次出现的 twitter 用户名。

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. asenovmanswer is simple, but will not work in most user input contexts, as techexpertis explaining in his comment.

做这个伎俩的正则表达式是/(^|[^@\w])@(\w{1,15})\b/. 我正在捕捉@ 字符后面的内容,以便我可以正确替换用户名。我正在使用全局匹配标志 (g),因此它将替换所有出现的。 asenovm答案很简单,但在大多数用户输入上下文中不起作用,正如techexpert他的评论中解释的那样。

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

output = text.replace( regex, replace );

console.log ( output );

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

这给了我我所期望的(用节点 v0.9.1 测试)

@RayFrancois answering to @AnPel, this is a real '@username83' but this is [email protected], and this is a @probablyfaketwitterusername

@RayFranco正在回复@AnPel,这是一个真正的“ @username83”,但这是 [email protected],这是一个 @probablyfaketwitterusername

This is based on Twitter "specs" for username:

这是基于用户名的 Twitter“规范”

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.

您的用户名不能超过 15 个字符。您的真实姓名可以更长(20 个字符),但为了方便起见,用户名会保持较短。如上所述,用户名只能包含字母数字字符(字母 AZ、数字 0-9),下划线除外。检查以确保您想要的用户名不包含任何符号、破折号或空格。

Hope this helps.

希望这可以帮助。

回答by Rodrigo Polo

A short an easy way to do it:

一个简单的方法来做到这一点:

function validTwitteUser(sn) {
    return /^[a-zA-Z0-9_]{1,15}$/.test(sn);
}

回答by suryaveer gaur

@[a-zA-Z0-9_]{0,15}

@[a-zA-Z0-9_]{0,15}

You can use above regular expression to sort of the twitter usernames from a mixed set of data

您可以使用上面的正则表达式从一组混合数据中对 Twitter 用户名进行排序

回答by Ernane Luis

I think the correct regex is this /^@(\w){1,15}/gwithout the ? on @(at)

我认为正确的正则表达式是/^@(\w){1,15}/g没有 ? 在@(at)

回答by Will Schoenberger

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.

它可能超出您的需要,但我在另一篇文章“正则表达式如何替换 twitter 链接”中找到了这一点。包裹@usernames、#hashtags 和 url。对我来说工作得很好。

function processTweetLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
    text = text.replace(exp, "<a href='' target='_blank'></a>");
    exp = /(^|\s)#(\w+)/g;
    text = text.replace(exp, "<a href='https://twitter.com/hashtag/?src=hash' target='_blank'>#</a>");
    exp = /(^|\s)@(\w+)/g;
    text = text.replace(exp, "<a href='http://www.twitter.com/' target='_blank'>@</a>");
    return text;
}

回答by Osman Turan

To exclude "non-latin"characters, you have to use: ^@?([a-zA-Z0-9_]){1,15}$. Because, \waccepts "any word characters". And non-latin characters qualifies this condition. So, it matches even ü?like Turkish characters as well.

要排除“非拉丁”字符,您必须使用:^@?([a-zA-Z0-9_]){1,15}$。因为,\w接受“任何单词字符”。非拉丁字符符合此条件。因此,它甚至ü?像土耳其语字符一样匹配。