javascript - Ignore white space in the string [closed]

0

I have this function in javascript and need to ignore the whitespace of the string. I think the function is removing the "blank spaces". Does anyone know how to use this same function without removing the blanks?

function getParameterByName(name) {
    if (name !== "" && name !== null && name != undefined) {
        name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
        var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace("/\+/g", " "));
    } else {
        var arr = location.href.split("/");
        return arr[arr.length - 1];
    }

}
    
asked by anonymous 24.08.2017 / 17:47

1 answer

1

When the paramenter name exists these will return the following:

return results === null ? "" : decodeURIComponent(results[1].replace("/\+/g", " "));

Then this line is comparing the result if it is not null will go through the function decodeURIComponent() and these will pass as argument results[1].replace("/\+/g", " " the same that removes the whitespace

You can simply substitute results[1] and your problem will be resolved

function getParameterByName(name) {
    if (name !== "" && name !== null && name != undefined) {
        name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
        var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : results[1];
    } else {
        var arr = location.href.split("/");
        return arr[arr.length - 1];
    }
}
    
24.08.2017 / 17:53