Remove all dots except the first one from a string

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

    Not sure what is supposed to happen if "." is the first character, I'd check for -1 in indexOf, also if you use substr once might as well use it twice.

    if ( index != -1 ) {
        input = input.substr( 0, index + 1 ) + input.substr(index + 1).replace( /\./g, '' );
    }
    

提交回复
热议问题