Remove all dots except the first one from a string

前端 未结 12 2593
栀梦
栀梦 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 07:06

    var i = s.indexOf(".");
    var result = s.substr(0, i+1) + s.substr(i+1).replace(/\./g, "");
    

    Somewhat tricky. Works using the fact that indexOf returns -1 if the item is not found.

提交回复
热议问题