Javascript正则表达式:查找后面不跟空格字符的单词
我需要 javascript 正则表达式来匹配后面没有空格字符并且之前有 @ 的单词,如下所示:
@bug - 找到“@bug”,因为它后面没有空格
@bug 和我 - 找不到任何东西,因为“后面有空格” @bug”
@bug 和 @another - 只找到“@another”
@bug 和 @another 以及其他东西 - 什么也没找到,因为这两个词后面都跟有空格。
帮助? 额外: 从中获取字符串,FF 将其自己的标签放在其末尾。虽然我基本上只需要以@开头的最后一个单词,但不能使用$(字符串结尾)。
I need javascript regex that will match words that are NOT followed by space character and has @ before, like this:
@bug - finds "@bug", because no space afer it
@bug and me - finds nothing because there is space after "@bug"
@bug and @another - finds "@another" only
@bug and @another and something - finds nothing because both words are followed by space.
Help?
Added:
string is fetched from and FF puts it's own tags at end of it. Although I basically need only the last word starting with @, $ (end-of-string) can not be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
re = /@\w+\b(?! )/
。这会查找一个单词(确保它捕获整个单词)并使用负向前查找来确保该单词后面没有空格。使用上面的设置:
唯一不起作用的方法是,如果您的单词以下划线结尾,并且您希望标点符号成为单词的一部分:例如“@bug and @another_ blahblah”将挑选出@another,因为< code>@another 后面没有空格。
这似乎不太可能,但如果您也想处理这种情况,您可以使用
/@\w+\b(?![\w ]/
,这将返回null
代表@bug 和@another_
,@bug_
代表@another 和@bug_
。Try
re = /@\w+\b(?! )/
. This looks for a word (making sure it captures the whole word) and uses a negative lookahead to make sure the word is not followed by a space.Using the setup above:
The only way this won't work is if your word ends in an underscore and you wanted that punctuation to be part of the word: For example '@bug and @another_ blahblah' will pick out @another since
@another
wasn't followed by a space.This doesn't seem very likely but if you wanted to deal with that case too, you could use
/@\w+\b(?![\w ]/
and that would returnnull
for@bug and @another_
and@bug_
for@another and @bug_
.听起来您实际上只是在输入末尾查找单词:
测试:
It sounds like you're really just looking for words at the end of the input:
Tests: