how to escape underscore in the URL using encodeURIComponent

409 views Asked by At

i've been working on my code. i wanted to encode my url that includes an underscore, but my code is not working. i've use encodeURICopmponent and it seems that it doesnt include underscore.

requestUrl: encodeURIComponent(uri).replace(/%5F/g),
requestorUrl: document.referrer ? encodeURIComponent(document.referrer) : encodeURIComponent(uri.origin),
1

There are 1 answers

0
AG1 On

You shouldn't encode undescore. But if you want to do it anyway, you can write

s.replace(/_/g, '%5F'); // transform all "_" to encoded "%5F"

To decode:

s.replace(/%5F/g, '_'); // transform encoded "%5F" to decoded "_"