[removed] replace last occurrence of text in a string

后端 未结 14 773
一整个雨季
一整个雨季 2020-12-02 18:13

See my code snippet below:

var list = [\'one\', \'two\', \'three\', \'four\'];
var str = \'one two, one three, one four, one\';
for ( var i = 0; i < list.         


        
相关标签:
14条回答
  • 2020-12-02 18:25

    Not as elegant as the regex answers above, but easier to follow for the not-as-savvy among us:

    function removeLastInstance(badtext, str) {
        var charpos = str.lastIndexOf(badtext);
        if (charpos<0) return str;
        ptone = str.substring(0,charpos);
        pttwo = str.substring(charpos+(badtext.length));
        return (ptone+pttwo);
    }
    

    I realize this is likely slower and more wasteful than the regex examples, but I think it might be helpful as an illustration of how string manipulations can be done. (It can also be condensed a bit, but again, I wanted each step to be clear.)

    0 讨论(0)
  • 2020-12-02 18:30

    If speed is important, use this:

    /**
     * Replace last occurrence of a string with another string
     * x - the initial string
     * y - string to replace
     * z - string that will replace
     */
    function replaceLast(x, y, z){
        var a = x.split("");
        var length = y.length;
        if(x.lastIndexOf(y) != -1) {
            for(var i = x.lastIndexOf(y); i < x.lastIndexOf(y) + length; i++) {
                if(i == x.lastIndexOf(y)) {
                    a[i] = z;
                }
                else {
                    delete a[i];
                }
            }
        }
    
        return a.join("");
    }
    

    It's faster than using RegExp.

    0 讨论(0)
  • 2020-12-02 18:32

    Here's a method that only uses splitting and joining. It's a little more readable so thought it was worth sharing:

        String.prototype.replaceLast = function (what, replacement) {
            var pcs = this.split(what);
            var lastPc = pcs.pop();
            return pcs.join(what) + replacement + lastPc;
        };
    
    0 讨论(0)
  • 2020-12-02 18:32

    Thought I'd answer here since this came up first in my Google search and there's no answer (outside of Matt's creative answer :)) that generically replaces the last occurrence of a string of characters when the text to replace might not be at the end of the string.

    if (!String.prototype.replaceLast) {
        String.prototype.replaceLast = function(find, replace) {
            var index = this.lastIndexOf(find);
    
            if (index >= 0) {
                return this.substring(0, index) + replace + this.substring(index + find.length);
            }
    
            return this.toString();
        };
    }
    
    var str = 'one two, one three, one four, one';
    
    // outputs: one two, one three, one four, finish
    console.log(str.replaceLast('one', 'finish'));
    
    // outputs: one two, one three, one four; one
    console.log(str.replaceLast(',', ';'));
    
    0 讨论(0)
  • 2020-12-02 18:32
    str = (str + '?').replace(list[i] + '?', 'finish');
    
    0 讨论(0)
  • 2020-12-02 18:33

    A simple answer without any regex would be:

    str = str.substr(0, str.lastIndexOf(list[i])) + 'finish'
    
    0 讨论(0)
提交回复
热议问题