简体   繁体   中英

How to remove a word that starts with a certain character from a string in Javascript

I have a string like

'here is some #tweet from @someone to @somebody-else'

and I need to remove all the words (and-phrases) that start with @mentions and return the string without them, so that it becomes

'here is some #tweet from to'

How to do that in Javascript?

Thank you!

var str = 'here is some #tweet from @someone to @somebody-else';

str = str.replace(/\s?@\S+/g, '');
var s = 'here is some #tweet from @someone to @somebody-else';
s = s.replace(/\s?@[\w-]+/g, "");
//here is some #tweet from to 

DEMO:

http://regex101.com/r/aV0hF2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM