Reputation: 2084
I'm looking to parse through a string and find all the handles (@name) and push them each into one array (without the @
though) so I can loop through them (with forEach
) and send them each an alert. Each handle is separated by a space.
Upvotes: 2
Views: 81
Reputation: 92427
try
let str= "Here @ann and @john go to @jane";
let m= str.match(/@\w+/g).map(x=>x.replace(/./,''));
m.forEach(x=> console.log(x));
You can use also positive lookbehind regexp but it is not supported by firefox yet (but it is part of ES2018):
let str= "Here @ann and @john go to @jane";
let m= str.match(/(?<=@)\w+/g);
m.forEach(x=> console.log(x));
where (?<=@)\w+
match word which start after @ (excluding this char - positive lookbehind)
Upvotes: 1
Reputation: 12959
Try This:
var str= "Here @ann and @john go to @jane";
var patt = /@(\w+)/g;
while ( (arr = patt.exec(str)) !== null ) { console.log(arr[1]); }
Upvotes: 0
Reputation: 15461
You can combine match
to extract names and slice
to remove @
:
str = "@JohnSmith @DylanThompson Hey guys";
let arr = str.match(/@\w+/g).map(e=>e.slice(1));
console.log(arr);
Upvotes: 1
Reputation: 37755
You can capture @followedByName
and than replace @
let str = `@someName hey @someMoreNames`
let op = str.match(/(^|\s)@\w+/g).map(e=>e.trim().replace(/@/g,''))
console.log(op)
Upvotes: 2
Reputation: 1514
If you just need to extract the users from a tweet, you can use the following regex:
/@([a-zA-Z0-9]+)/g
For example:
var string = '@JohnSmith @DylanThompson Hey guys!';
var numberPattern = /@([a-zA-Z0-9]+)/g;
var res = string.match(numberPattern);
console.log(res);
This would spit out:
["@JohnSmith", "@DylanThompson"]
Upvotes: 2