Javascript正则表达式:查找后面不跟空格字符的单词

发布于 2024-12-22 09:17:46 字数 305 浏览 5 评论 0原文

我需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2024-12-29 09:17:46

尝试re = /@\w+\b(?! )/。这会查找一个单词(确保它捕获整个单词)并使用负向前查找来确保该单词后面没有空格。

使用上面的设置:

var re = /@\w+\b(?! )/, // etc etc

for ( var i=0; i<cases.length; i++ ) {
    print( re2.exec(cases[i]) )
}

//prints
@bug
null
@another
null

唯一不起作用的方法是,如果您的单词以下划线结尾,并且您希望标点符号成为单词的一部分:例如“@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:

var re = /@\w+\b(?! )/, // etc etc

for ( var i=0; i<cases.length; i++ ) {
    print( re2.exec(cases[i]) )
}

//prints
@bug
null
@another
null

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 return null for @bug and @another_ and @bug_ for @another and @bug_.

一腔孤↑勇 2024-12-29 09:17:46

听起来您实际上只是在输入末尾查找单词:

/@\w+$/

测试:

var re = /@\w+$/,
    cases = ['@bug',
             '@bug and me',
             '@bug and @another',
             '@bug and @another and something'];

for (var i=0; i<cases.length; i++)
{
    console.log(cases[i], ':', re.test(cases[i]), re.exec(cases[i]));
}

// prints
@bug : true ["@bug"]
@bug and me : false null
@bug and @another : true ["@another"]
@bug and @another and something : false null

It sounds like you're really just looking for words at the end of the input:

/@\w+$/

Tests:

var re = /@\w+$/,
    cases = ['@bug',
             '@bug and me',
             '@bug and @another',
             '@bug and @another and something'];

for (var i=0; i<cases.length; i++)
{
    console.log(cases[i], ':', re.test(cases[i]), re.exec(cases[i]));
}

// prints
@bug : true ["@bug"]
@bug and me : false null
@bug and @another : true ["@another"]
@bug and @another and something : false null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文