Reputation: 997
I got a string like this one:
var tweet ="@fadil good:))RT @finnyajja: what a nice day RT @fadielfirsta: how are you? @finnyajja yay";
what kind of code should work to extract any words with @ character and also removing any special char at the end of the words? so it would an array like this :
(@fadil, @finnyajja, @fadielfirsta, @finnyajja);
i have tried the following code :
var users = $.grep(tweet.split(" "), function(a){return /^@/.test(a)});
it returns this:
(@fadil, @finnyajja:, @fadielfirsta:, @finnyajja)
there's still colon ':' character at the end of some words. What should I do? any solution guys? Thanks
Upvotes: 1
Views: 40
Reputation: 7133
Here is code that is more straightforward than trying to use split:
var tweet_text ="@fadil good:))RT @finnyajja: what a nice day RT @fadielfirsta: how are you? @finnyajja yay";
var result = tweet_text.match(/@\w+/g);
Upvotes: 2
Reputation: 78650
The easiest way without changing your current code too much would be to just remove all colons prior to calling split:
var users = $.grep(tweet_text.replace(":","").split(" "), function(a){return /^@/.test(a)});
You could also write a regex to do all the work for you using match
. Something like this:
var regex = /@[a-z0-9]+/gi;
var matches = tweet.match(regex);
This assumes that you only want letters and numbers, if certain other characters are allowed, this regex will need to be modified.
Upvotes: 2