简体   繁体   English

从jquery / javascript中的字符串中获取特定单词

[英]Get specific word from string in jquery/javascript

I have string, for example: 我有字符串,例如:

$string = 'username1, username2 and username3 like this posts';

I want to get only text contains 'username' and delete everything else. 我只想获取包含“用户名”的文本,并删除其他所有内容。 So after delete it will be only 'username1 username2 username3' . 因此,删除后将仅是'username1 username2 username3'

You can use the /username\\d/g to match the username followed by a digit which will give you the array of matched words then use join(' ') , join with a space to get the desired output: 您可以使用/username\\d/g来匹配username后跟一个数字,该数字将为您提供匹配单词的数组,然后使用join(' ') ,并加一个空格以获取所需的输出:

 var string = 'username1, username2 and username3 like this posts'; var res = string.match(/username\\d+/g).join(' '); console.log(res); 

it will help you to get your character. 它将帮助您获得角色。 var str = 'username1, username2 and username3 like this posts'.replace('username1, username2 and username3 like this posts','username') alert(str); var str ='像这样的帖子的用户名1,用户名2和用户名3'.replace('像这样的帖子的用户名1,用户名2和用户名3','用户名')alert(str);

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

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