我想在java脚本中找到以@开头的所有单词
句子:"嘿@dinesh,我在@dean vent"中提到过你;
现在使用上面的句子,我想找到以attherate(@)开头的两个单词(dinesh和dean)。为了更方便,您可以假设Twitter功能,其中任何用户都提到了其他用户@。
如果有正则表达或功能请告诉我
感谢。
答案 0 :(得分:4)
var atmentions = "Hey @dinesh, I have mentioned you in @dean vent".match(/@\w+/g)
// gives ["@dinesh", "@dean"]
如果您想要的用户名没有' @'你可以像这样转换它们
atmentions.map( function( name ){ return name.substring(1); })
// ["dinesh", "dean"]
答案 1 :(得分:1)
你可以这样做:
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"];
答案 2 :(得分:0)
可以这样做:
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"]