Replace multiple strings with multiple other strings

前端 未结 18 2035
别那么骄傲
别那么骄傲 2020-11-22 04:14

I\'m trying to replace multiple words in a string with multiple other words. The string is \"I have a cat, a dog, and a goat.\"

However, this does not produce \"I ha

18条回答
  •  遥遥无期
    2020-11-22 04:47

    String.prototype.replaceSome = function() {
        var replaceWith = Array.prototype.pop.apply(arguments),
            i = 0,
            r = this,
            l = arguments.length;
        for (;i

    /* replaceSome method for strings it takes as ,much arguments as we want and replaces all of them with the last argument we specified 2013 CopyRights saved for: Max Ahmed this is an example:

    var string = "[hello i want to 'replace x' with eat]";
    var replaced = string.replaceSome("]","[","'replace x' with","");
    document.write(string + "
    " + replaced); // returns hello i want to eat (without brackets)

    */

    jsFiddle: http://jsfiddle.net/CPj89/

提交回复
热议问题