var string = "user1,user2,user1,user3,user4,user1,";
I want to remove all 'user1,' from the string but by 'replace' method I can just remove one of them.
var string = "user1,user2,user1,user3,user4,user1,";
I want to remove all 'user1,' from the string but by 'replace' method I can just remove one of them.
try out this..
var string = "user1,user2,user1,user3,user4,user1,";
string.replace(/user1,/g, '');
alert('string .. '+string);
var str= "user1,user2,user1,user3,user4,user1,";
str = str.replace(/user1,/g, ''); //replaces all 'user1,'
First parameter replace method can be regular expression. Use option 'g' for replace all matches.
var string = "user1,user2,user1,user3,user4,user1,";
string = string.replace(/user1,/g, '');
use regexp
var string = "user1,user2,user1,user3,user4,user1,";
string.replace(/user1/g, '');
EDITED CODE
var string = "user1,user2,user1,user3,user4,user1,";
var find = 'user1';
var re = new RegExp(find, 'g');
string = string.replace(re, '');
try this, split the string, get the array, filter it to remove duplicate items, join them back.
var uniqueList=string.split(',').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(',');