Remove all dots except the first one from a string

前端 未结 12 2601
栀梦
栀梦 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条回答
  •  Happy的楠姐
    2020-12-15 06:53

    You can try something like this:

    str = str.replace(/\./,"#").replace(/\./g,"").replace(/#/,".");
    

    But you have to be sure that the character # is not used in the string; or replace it accordingly.

    Or this, without the above limitation:

    str = str.replace(/^(.*?\.)(.*)$/, function($0, $1, $2) {
      return $1 + $2.replace(/\./g,"");
    });
    

提交回复
热议问题