URIError: malformed URI sequence?

33,551

I think you need to URI encode the percentage sign as '%25'

http://some-external-server.com/info?progress=60%25%20-%20Completed 

[EDIT]

I guess you could do something like this:

var str = "60%%20-%20completed";
var uri_encoded = str.replace(/%([^\d].)/, "%25$1");
console.log(str); // "60%25%20-%20completed"
var decoded = decodeURIComponent(uri_encoded);
console.log(decoded); // "60% - completed"
Share:
33,551
Stacked
Author by

Stacked

Updated on July 09, 2022

Comments

  • Stacked
    Stacked almost 2 years

    The below code error's out with URIError: malformed URI sequence? when there is a % sign like 60% - Completed in the URL string from where I need to extract the parameter value e.g. http://some-external-server.com/info?progress=60%%20-%20Completed

       <SCRIPT type="text/javascript">
                function getParameterByName(name) {
                    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                    results = regex.exec(location.search);
                    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
                }
        </SCRIPT>
    

    I dont have control of the server and need to process the output in my html page.

  • Stacked
    Stacked over 10 years
    As mentioned above - I dont have control of the server and need to process the output in my html page.
  • Slicedpan
    Slicedpan over 10 years
    You may not be able to use decodeURIComponent then, or you may need to preprocess the url before passing it to that function. Are spaces the only other special character you can expect in the url?
  • Stacked
    Stacked over 10 years
    No, there could be other special characters in the URL. Anything which would let me extract parameter value in this condition?
  • Stacked
    Stacked over 10 years
    I tried to use this on the getParameterByName function but can't seem to get it working. Can you provide an example in the above code's context for a newbie.
  • Stacked
    Stacked over 10 years
    Here is the code : <SCRIPT type="text/javascript"> function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); var uri_encoded = results[1].replace(/%([^\d].)/, "%25$1"); return results == null ? "" : decodeURIComponent(uri_encoded.replace(/\+/g, " ")); } </SCRIPT>
  • Stacked
    Stacked over 10 years
    TypeError: results is null on line var uri_encoded = results[1].replace(/%([^\d].)/, "%25$1");.
  • Slicedpan
    Slicedpan over 10 years
    the value of results is null. you need to check for this before the line var uri_encoded = ...