Why doesn't decodeURI(“a+b”) == “a b”?

前端 未结 4 1487
执念已碎
执念已碎 2020-12-30 00:48

I\'m trying to encode URLs in Ruby and decode them with Javascript. However, the plus character is giving me weird behavior.

In Ruby:

[Dev]> CGI.e         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-30 01:28

    You might want to look at URI.encode and URI.decode:

    require 'uri'
    
    URI.encode('a + b') # => "a%20+%20b"
    URI.decode('a%20+%20b') # => "a + b"
    

    An alternate, that I use a lot, is Addressable::URI:

    require 'addressable/uri'
    Addressable::URI.encode('a + b') #=> "a%20+%20b"
    Addressable::URI.unencode('a%20+%20b') #=> "a + b"
    

提交回复
热议问题