简体   繁体   English

如何从Javascript中的字符串中删除以某个字符开头的单词

[英]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 并且我需要删除所有以@mentions开头的单词(和短语),并返回不包含它们的字符串,以使其变为

'here is some #tweet from to'

How to do that in Javascript? 如何在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 http://regex101.com/r/aV0hF2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串中删除以某个字符开头的单词 - Remove word from string beginning with a certain character 从Javascript中的字符串中提取以某个字符开头的单词 - Extract words that starts with a certain character from a string in Javascript 如何在javascript中删除字符串末尾的某个字符 - how to remove a certain character at the end of the string in javascript 如何从 JavaScript 中的某些子字符串(以列表形式给出)以外的字符串中删除每个字符? - How to remove every character from a string except certain substrings (given as a list) in JavaScript? 当字符串中单词的字符长度小于数组中字符的长度时,如何根据Javascript中的数组删除字符串中的单词? - How to remove word in string based on array in Javascript when word's character length in string is fewer than in array? 从JavaScript字符串中删除字符/ - remove the character / from a javascript string 如何使用javascript从某个字符中提取字符串? - How to extract the string from certain character using javascript? 从某个字符javascript替换字符串 - replace string from a certain character javascript 如何使用JavaScript从字符串中删除第一个单词“ the” - How to remove first word “the” from string using javascript 如何使用JavaScript从div内容中删除单词/字符串 - How to remove a word/string from div content using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM