Removing duplicate comma separated list with regex?

I am trying to figure out how to filter out duplicates in a regex string, where the string is separated by a comma. I would like to do this in javascript, but I was catching up on how to use backlinks.

For example:

1,1,1,2,2,3,3,3,3,4,4,4,5

becomes:

1,2,3,4,5

Or:

a,b,b,said,said, t, u, ugly, ugly

becomes

a,b,said,t,u,ugly
+5
source share
5 answers

Why use regex when you can do this in javascript code? Here is a sample code (dirty though):

var input = 'a,b,b,said,said, t, u, ugly, ugly';
var splitted = input.split(',');
var collector = {};
for (i = 0; i < splitted.length; i++) {
   key = splitted[i].replace(/^\s*/, "").replace(/\s*$/, "");
   collector[key] = true;
}
var out = [];
for (var key in collector) {
   out.push(key);
}
var output = out.join(','); // output will be 'a,b,said,t,u,ugly'

p / s: one regular expression in a for-loop should clip tokens, not make them unique

+8
source

If you insist on RegExp, here is an example in Javascript:

"1,1,1,2,2,3,3,3,3,4,4,4,5".replace (
    /(^|,)([^,]+)(?:,\2)+(,|$)/ig, 
    function ($0, $1, $2, $3) 
    { 
        return $1 + $2 + $3; 
    }
);

To handle space trimming, slightly modify:

"1,1,1,2,2,3,3,3,3,4,4,4,5".replace (
    /(^|,)\s*([^,]+)\s*(?:,\s*\2)+\s*(,|$)\s*/ig, 
    function ($0, $1, $2, $3) 
    { 
        return $1 + $2 + $3; 
    }
);

, split .

+1

:

s/,([^,]+),\1/,$1/g;

Perl, JS- , .

0

.

. , , , .

. CSV, , Split . , CSV, CSV-.

function GetUniqueItems(s)
{
    var items=s.split(",");

    var uniqueItems={};

    for (var i=0;i<items.length;i++)
    {           
        var key=items[i];
        var val=items[i];
        uniqueItems[key]=val;
    }

    var result=[];

    for(key in uniqueItems)
    {
        // Assign to output result field using hasOwnProperty so we only get 
        // relevant items
        if(uniqueItems.hasOwnProperty(key))
        {
            result[result.length]=uniqueItems[key];
        }
    }    
    return result;
}
0

javascript regex

x="1,1,1,2,2,3,3,3,3,4,4,4,5"

while(/(\d),\1/.test(x))
    x=x.replace(/(\d),\1/g,"$1")

1,2,3,4,5


x="a,b,b,said,said, t, u, ugly, ugly"

while(/\s*([^,]+),\s*\1(?=,|$)/.test(x))
    x=x.replace(/\s*([^,]+),\s*\1(?=,|$)/g,"$1")

a,b,said, t, u,ugly

, , - .

0

All Articles