Remove all dots except the first one from a string

Posted by Šime Vidas on Stack Overflow See other posts from Stack Overflow or by Šime Vidas
Published on 2011-11-15T17:34:55Z Indexed on 2011/11/15 17:50 UTC
Read the original article Hit count: 319

Filed under:
|

Given a string

'1.2.3.4.5'

I would like to get this output

'1.2345'

I wrote this

function process( input ) {
    var index = input.indexOf( '.' );

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

    return input;
}

Live demo: http://jsfiddle.net/EDTNK/

It works but I was hoping for a slightly more elegant solution...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about string