I'm grabbing the query string parameters and trying to do this:
var hello = unescape(helloQueryString);
and it returns:
this+is+the+string
instead of:
this is the string
Works great if %20's were in there, but it's +'s. Any way to decode these properly so they + signs move to be spaces?
Thanks.
Approach: In the given string Str, replace all occurrences of Sub with empty spaces. Remove unwanted empty spaces in start and end of the string. Print the modified string.
According to the W3C (and they are the official source on these things), a space character in the query string (and in the query string only) may be encoded as either " %20 " or " + ".
Use the String. replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.
In the above code, we have passed two arguments to the replace() method first one is regex /\s/g and the second one is replacement value + , so that the replace() method will replace all-white spaces with a + sign. The regex /\s/g helps us to remove the all-white space in the string.
The decodeURIComponent
function will handle correctly the decoding:
decodeURIComponent("this%20is%20the%20string"); // "this is the string"
Give a look to the following article:
escape()
, encodeURI()
, and encodeURIComponent()
Adding this line after would work:
hello = hello.replace( '+', ' ' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With