سؤال

So, I've been writing a twitter client using node.js and Javscript using node-webkit.

And, eventually I've gotten to the point that retweets are passed on in text.

RT @somename: status

I've tried finding some sort of regular expression to replace RT @someone:, in it's entirety with nothing. But I haven't been able to find anything.

I'm not great with, nor do I understand regular expressions so any help would be appreciated!

هل كانت مفيدة؟

المحلول

You can use the following.

var str = '@foo @bar @baz RT @somename: status',
    res = str.replace(/RT\s*@\S+/g, '');

console.log(res); // => "@foo @bar @baz  status"

Regular expression:

RT             'RT'
\s*            whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 @             '@'
 \S+           non-whitespace (all but \n, \r, \t, \f, and " ") (1 or more times)

Another option would be just to match up until and include the colon.

str.replace(/RT\s*@[^:]*:/g, '');

نصائح أخرى

How about this:

var text = 'RT @somename: status';
newstr = text.replace(/RT @.+?:/g, '');
document.getElementById('textareaFilter').value = newstr;

Here's an example jsFiddler

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top