Percent encoding javascript

后端 未结 4 2008
挽巷
挽巷 2021-02-02 10:55

Is there a javascript function that takes a string and converts it into another string that is percent-encoded? That way something like \"This Guy\" turns into \"This%20Guy\".

4条回答
  •  不知归路
    2021-02-02 11:26

    Try this encodeURIComponent()

    var stringToDecode = "J&K";
    
    var encodedString = encodeURIComponent(stringToDecode );
    

    Use decodeURIComponent() to decode it again when needed

    More Info here

    https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

提交回复
热议问题