【发布时间】:2026-01-14 08:25:02
【问题描述】:
在下面的文本中,什么正则表达式(Javascript)会匹配“用户”(用户是一个随机名称),不包括“@”字符?
I want to tag this @user here
and this @user
@user
我查看了以下解决方案并制作了以下不起作用的正则表达式
RegEx pattern to match a string between two characters, but exclude the characters
\@(.*)\s
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(?!\@)(.*?)(?=\s)
Regex: Matching a character and excluding it from the results?
^@[^\s]+
最后我制作了这个有效但返回“@user”而不是“user”的正则表达式:
@[^\s\n]+
用于执行正则表达式的 Javascript 是:
string.match(/@[^\s\n]+/)
【问题讨论】:
-
你如何调用正则表达式?这就是问题所在。
-
似乎至少部分地,问题不仅在于正则表达式,还在于您如何在代码中使用它。您提到的最后一个表达式应该可以匹配用户名但不能提取它。要提取它,您需要形成一个带括号的捕获组,例如
@([^\s\n]+)。 -
发布您的确切 JavaScript。
标签: javascript regex