nosdalg
nosdalg

Reputation: 571

Extend encodeURIComponent such as to skip some values from encoding

Having a URL such as .

var url = `damen/hosen-roecke/7%2F8-laenge`

While encoding encodeURIComponent(url) it returns damen%2Fhosen-roecke%2F7%252F8-laenge

But I need to skip this particular part 7%2F8 from encoding which is already encoded once. So I guess how to override encodeURIComponent so that it can skip this particular format '7%2F8'

So that it can work like after encoding it should return damen%2Fhosen-roecke%2F7%2F8-laenge

Upvotes: 0

Views: 739

Answers (1)

Tushar
Tushar

Reputation: 87203

As the string contain some part that is encoded, first decode the string and then encode again.

encodeURIComponent(decodeURIComponent(url)) // "damen%2Fhosen-roecke%2F7%2F8-laenge"

var url = `damen/hosen-roecke/7%2F8-laenge`;
document.body.innerHTML = encodeURIComponent(decodeURIComponent(url));

Steps:

  1. decodeURIComponent(url) will give completely decoded string "damen/hosen-roecke/7/8-laenge"
  2. encodeURIComponent("damen/hosen-roecke/7/8-laenge") will give the "damen%2Fhosen-roecke%2F7%2F8-laenge" string.

Upvotes: 2

Related Questions