Remove all dots except the first one from a string

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

    You could also do something like this, i also don't know if this is "simpler", but it uses just indexOf, replace and substr.

    var str = "7.8.9.2.3";
    var strBak = str;
    
    var firstDot = str.indexOf(".");
    str = str.replace(/\./g,"");
    str = str.substr(0,firstDot)+"."+str.substr(1,str.length-1);
    
    document.write(str);
    

    Shai.

提交回复
热议问题