Javascript String replaceAll(strTarget, strSubString)

Description

Javascript String replaceAll(strTarget, strSubString)


// Replaces all instances of the given substring.
String.prototype.replaceAll = function (strTarget, strSubString) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);
    while (intIndexOfMatch != -1) {
        strText = strText.replace(strTarget, strSubString);
        intIndexOfMatch = strText.indexOf(strTarget);
    }/* ww  w. ja va  2 s .  co m*/
    return ( strText );
};

// Test
var str;
str = 'I+love+you';
str = str.replaceAll("+", " ");



PreviousNext

Related