Javascript:要查找以开头的句子中的所有单词吗@

Javascript:要查找以开头的句子中的所有单词吗@,javascript,regex,Javascript,Regex,我想在java脚本中找到所有以@开头的单词 句子:“嘿,迪内什,我在@dean vent中提到过你” 现在使用上面的句子,我想找到两个以attherate(@)开头的单词(dinesh和dean)。为了更方便,您可以假设twitter的功能,其中任何用户都可以使用@提及其他用户 如果有正则表达式或函数,请告诉我 谢谢。您可以: function findWords(words) { return words.split(" ").filter(function(word) {

我想在java脚本中找到所有以@开头的单词

句子:“嘿,迪内什,我在@dean vent中提到过你”

现在使用上面的句子,我想找到两个以attherate(@)开头的单词(dinesh和dean)。为了更方便,您可以假设twitter的功能,其中任何用户都可以使用@提及其他用户

如果有正则表达式或函数,请告诉我

谢谢。

您可以:

function findWords(words) {
    return words.split(" ").filter(function(word) {
        if (word.indexOf("@") === 0) return word
    });
}

var words = findWords("Hey @dinesh, I have mentioned you in @dean vent");
console.log(words); //["@dinesh", "@dean"];
小提琴:你可以做:

function findWords(words) {
    return words.split(" ").filter(function(word) {
        if (word.indexOf("@") === 0) return word
    });
}

var words = findWords("Hey @dinesh, I have mentioned you in @dean vent");
console.log(words); //["@dinesh", "@dean"];
小提琴:

如果希望用户名不带“@”,则可以这样转换它们

atmentions.map( function( name ){ return name.substring(1); })
// ["dinesh", "dean"]
如果希望用户名不带“@”,则可以这样转换它们

atmentions.map( function( name ){ return name.substring(1); })
// ["dinesh", "dean"]
这可以通过以下方式完成:

var sentence = "Hey @dinesh, I have mentioned you in @dean vent",
    result = [];
sentence.split(" ").forEach(function(word) {
     if(word.indexOf("@") == 0) {
          result.push(word);         // or result.push(word.substring(1)) to skip @.
     }
});
console.log(result);    //["@dinesh", @dean"]
这可以通过以下方式完成:

var sentence = "Hey @dinesh, I have mentioned you in @dean vent",
    result = [];
sentence.split(" ").forEach(function(word) {
     if(word.indexOf("@") == 0) {
          result.push(word);         // or result.push(word.substring(1)) to skip @.
     }
});
console.log(result);    //["@dinesh", @dean"]

使用捕获组
@(\S+)
@(\w+)
。从组索引1中获取名称。请在询问此类问题之前进行搜索。使用捕获组
@(\S+)
@(\w+)
。从组索引1中获取名称。请在询问此类问题之前进行搜索。这实际上包括第一次匹配后的逗号。正则表达式的答案更合适,因为它更容易找到单词边界。这实际上包括第一次匹配后的逗号。正则表达式的答案更合适,因为它更容易找到单词边界