Removing duplicates in a comma-separated list with a regex?

前端 未结 5 1525
滥情空心
滥情空心 2020-12-18 07:45

I\'m trying to figure out how to filter out duplicates in a string with a regular expression, where the string is comma separated. I\'d like to do this in javascript, but I\

5条回答
  •  盖世英雄少女心
    2020-12-18 08:11

    I don't use Regular Expressions for that.

    Here's the function I use. It accepts a string containing comma separated values and returns an array of unique values regardless of position in the original string.

    Note: If you pass CSV string containing quoted values, Split will not treat commas inside quoted values any differently. So if you want to handle real CSV, you are best to use a 3rd party CSV parser.

    function GetUniqueItems(s)
    {
        var items=s.split(",");
    
        var uniqueItems={};
    
        for (var i=0;i

提交回复
热议问题