Remove all dots except the first one from a string

前端 未结 12 2619
栀梦
栀梦 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:59

    Here is another approach:

    function process(input) {
        var n = 0;
        return input.replace(/\./g, function() { return n++ > 0 ? '' : '.'; });
    }
    

    But one could say that this is based on side effects and therefore not really elegant.

提交回复
热议问题