第一个给定一个字符串删除所有的点,除了从字符串
'1.2.3.4.5'
我想获得这个输出
'1.2345'
(如果有字符串中没有点,字符串应该原封不动地返回。)
我写这
function process(input) {
var index = input.indexOf('.');
if (index > -1) {
input = input.substr(0, index + 1) +
input.slice(index).replace(/\./g, '');
}
return input;
}
现场演示:http://jsfiddle.net/EDTNK/1/
它的工作原理,但我希望一个稍微更优雅的解决方案......
我觉得你现有的解决方案也相当考究加入 –
我同意 - 你的解决方案似乎没什么问题 –
@迈克尔的解决方案使用'indexOf','substr'。 ,'slice'和'replace',加上'if'语句,我觉得它不是t尽可能简单...... –