What can i use instead of replaceall in javascript?

I am coding a template for eBay. However, eBay does not allow .replace. The code below is for a rollover tab section.When the user hovers over tab(a), the correspodning div div(a) is made to become visible.

Is there a workaround to get the code to work without using .replace?

var divs = new Array();
divs.push("contentPayment");
divs.push("contentShipping");
divs.push("contentWarranty");
divs.push("contentContact");
var navs = new Array();
navs.push("nav1");
navs.push("nav2");
navs.push("nav3");
navs.push("nav4");
///////////////////////////////////////


function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
///////////////////////////////////////////////////////////////////////


function toggleDisplay(id) {
    for (var i = 0; i < divs.length; i++) {
        var item = document.getElementById(divs[i]);
        item.style.display = 'none';
    }
    var target = document.getElementById(id);
    target.style.display = 'block';
    /////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////PAYMENT IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentPayment") {
        var CurrentTab = document.getElementById("nav1");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Shipping IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentShipping") {
        var CurrentTab = document.getElementById("nav2");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Warranty IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentWarranty") {
        var CurrentTab = document.getElementById("nav3");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Contact IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentContact") {
        var CurrentTab = document.getElementById("nav4");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }
}

Luuklag

3,88911 gold badges39 silver badges55 bronze badges

asked Oct 7, 2012 at 15:18

2

You may try this as an alternative of replace function

String.prototype.fakeReplace = function(str, newstr) {
    return this.split(str).join(newstr);
};

var str = "Welcome javascript";
str = str.fakeReplace('javascript', '');
alert(str); // Welcome

DEMO.

answered Oct 7, 2012 at 15:32

The AlphaThe Alpha

140k27 gold badges283 silver badges303 bronze badges

3

For a more efficient, but slightly longer method, use this:

String.prototype.myReplace = function(pattern, nw) {
   var curidx = 0, len = this.length, patlen = pattern.length, res = "";
   while(curidx < len) {
       var nwidx = this.indexOf(pattern, curidx);
       console.log(nwidx);
       if(nwidx == -1) {
           break;
       }
       res = res + this.substr(curidx, nwidx - curidx);
       res = res + nw;
       curidx = nwidx + patlen;
   }
   return res;
};
alert("Glee is awesome".myReplace("awesome", "very very very awesome"));

See it in action: little link.

Hope that helped!

answered Oct 8, 2012 at 9:55

What can i use instead of replaceall in javascript?

ChrisChris

26.2k5 gold badges56 silver badges71 bronze badges

1

Does JavaScript have replaceAll?

JavaScript String replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement.

Why replaceAll is not working in JS?

The "replaceAll" is not a function error occurs when we call the replaceAll() method on a value that is not of type string, or in a browser that doesn't support the method. To solve the error, only call the replaceAll() method on strings in supported browsers.

What does public string replaceAll replace?

public String replaceAll(String regex, String replacement) The replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement.

What can I use instead of substring in JavaScript?

Introduction to the JavaScript String replace() method The JavaScript String replace() method returns a new string with a substring ( substr ) replaced by a new one ( newSubstr ). Note that the replace() method doesn't change the original string.