Remove all dots except the first one from a string

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

    You could also do something like this, i also don't know if this is "simpler", but it uses just indexOf, replace and substr.

    var str = "7.8.9.2.3";
    var strBak = str;
    
    var firstDot = str.indexOf(".");
    str = str.replace(/\./g,"");
    str = str.substr(0,firstDot)+"."+str.substr(1,str.length-1);
    
    document.write(str);
    

    Shai.

    0 讨论(0)
  • 2020-12-15 06:58
    var input = '14.1.2';
    reversed = input.split("").reverse().join("");
    reversed = reversed.replace(\.(?=.*\.), '' );
    input = reversed.split("").reverse().join("");
    
    0 讨论(0)
  • 2020-12-15 06:59

    Here is another approach:

    function process(input) {
        var n = 0;
        return input.replace(/\./g, function() { return n++ > 0 ? '' : '.'; });
    }
    

    But one could say that this is based on side effects and therefore not really elegant.

    0 讨论(0)
  • 2020-12-15 07:02

    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.

    0 讨论(0)
  • 2020-12-15 07:03

    Trying to keep this as short and readable as possible, you can do the following:

    JavaScript

    var match = string.match(/^[^.]*\.|[^.]+/g);
    string = match ? match.join('') : string;
    

    Requires a second line of code, because if match() returns null, we'll get an exception trying to call join() on null. (Improvements welcome.)

    Objective-J / Cappuccino (superset of JavaScript)

    string = [string.match(/^[^.]*\.|[^.]+/g) componentsJoinedByString:''] || string;
    

    Can do it in a single line, because its selectors (such as componentsJoinedByString:) simply return null when sent to a null value, rather than throwing an exception.

    As for the regular expression, I'm matching all substrings consisting of either (a) the start of the string + any potential number of non-dot characters + a dot, or (b) any existing number of non-dot characters. When we join all matches back together, we have essentially removed any dot except the first.

    0 讨论(0)
  • 2020-12-15 07:06
    var i = s.indexOf(".");
    var result = s.substr(0, i+1) + s.substr(i+1).replace(/\./g, "");
    

    Somewhat tricky. Works using the fact that indexOf returns -1 if the item is not found.

    0 讨论(0)
提交回复
热议问题