Ryan Claxton
Ryan Claxton

Reputation: 175

javascript string replace special characters

I have a function that removes the filter from an html string if a checkbox is unchecked or adds it if it is checked. I am having problems removing the %20 or + symbol from in front of the string I want to remove using the string replace function.

This code works:

var addFilter = change.value;

if (change.checked == true) { //add filter }
else {
    var removeFilter1 = addFilter;
    var removeFilter2 = addFilter;

        if(currFilter != '') {
            var url = currFilter.replace(removeFilter1, "");
            var url = url.replace(removeFilter2, "");
            window.open(url, "_self");
        }

}

...basically I tried setting removeFilter1 = "%20" + addFilter; and removeFilter2 = "+" + addFilter; to get rid of the extra space characters in the url but it doesn't do anything when I make that change. I'm pretty sure it has something to do with the replace function not working with special characters but not sure how to fix. It isnt really that big of a deal because it still works with the extra spaces included but its bothering me lol.

Upvotes: 2

Views: 911

Answers (2)

Parag Bhayani
Parag Bhayani

Reputation: 3330

%20 is basically space, it is shown like %20 in url to denote space as urls can't have space.. so you should try to remove just space not %20 from the string... e.g.

var url = currFilter.replace(" ", "");

Upvotes: 1

Tob
Tob

Reputation: 943

Have you tried decodeURI and replacing spaces?

Greets

Upvotes: 0

Related Questions