Remove all dots except the first one from a string

前端 未结 12 2611
栀梦
栀梦 2020-12-15 06:34

Given a string

\'1.2.3.4.5\'

I would like to get this output

\'1.2345\'

(In case there are no dots in the

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 06:51

    This isn't necessarily more elegant, but it's another way to skin the cat:

    var process = function (input) {
        var output = input;
    
        if (typeof input === 'string' && input !== '') {
            input = input.split('.');
            if (input.length > 1) {
                output = [input.shift(), input.join('')].join('.');
            }
        }
    
        return output;
    };
    

提交回复
热议问题