There is a pretty short solution (assuming input
is your string):
var output = input.split('.');
output = output.shift() + '.' + output.join('');
If input
is "1.2.3.4
", then output
will be equal to "1.234
".
See this jsfiddle for a proof. Of course you can enclose it in a function, if you find it necessary.
EDIT:
Taking into account your additional requirement (to not modify the output if there is no dot found), the solution could look like this:
var output = input.split('.');
output = output.shift() + (output.length ? '.' + output.join('') : '');
which will leave eg. "1234
" (no dot found) unchanged. See this jsfiddle for updated code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…