我需要javascript正则表达式,将匹配的字,后面没有空格字符和@之前,像这样:
@bug -查找"@bug",因为它后面没有空格
@bug and me -找不到任何东西,因为"@bug"后面有空格
@bug and @another -只查找"@another"
@bug和@another and something -找不到任何结果,因为这两个词后面都有空格。
帮助吗?补充道:字符串被获取,FF把它自己的标签放在它的末尾。虽然我基本上只需要以@开头的最后一个单词,但不能使用$ (end- string)。
尝试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和@another_ blahblah'将选择@another,因为@another
后面没有空格。这似乎不太可能,但如果你想处理这种情况下,你也可以使用/@w+b(?![w ]/
,这将返回null
为@bug and @another_
和@bug_
为@another and @bug_
。
听起来你只是在寻找输入末尾的单词:
/@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