# URI.escape\_component

**URL:** <https://www.ruby-forum.com/t/uri-escape-component/174550>\
**Category:** Development\
**Created:** [August 28, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/uri-escape-component/174550 "2009-08-28T14:09:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [August 28, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/uri-escape-component/174550/1 "2009-08-28T14:09:51Z")

</div>

ã€€JavaScript ã® escapeURIComponent() ç›¸å½“ã®é–¢æ•°ã£ã¦ã‚ã‚Šã¾ã—ãŸã£ã‘ï¼Ÿ  
ã‚‚ã—ãªã‘ã‚Œã°URIãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã«è¿½åŠ ã™ã‚‹ã®ã¯ã©ã†ã§ã™ã‹ã€‚

ã€€ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆã®ä¿®æ­£ã‚‚æ··ã–ã£ã¦ã„ã¾ã™ãŒã€ä»¥ä¸‹ãŒãƒ‘ãƒƒãƒã§ã™ã€‚

# Index: lib/uri/common.rb

— lib/uri/common.rb (revision 24684)  
+++ lib/uri/common.rb (working copy)  
@@ -70,6 +70,7 @@ module URI  
#  
# \* :ESCAPED (URI::PATTERN::ESCAPED in default)  
# \* :UNRESERVED (URI::PATTERN::UNRESERVED in default)

- 
# \* :RESERVED (URI::PATTERN::RESERVED in default)

# \* :DOMLABEL (URI::PATTERN::DOMLABEL in default)

# \* :TOPLABEL (URI::PATTERN::TOPLABEL in default)

# \* :HOSTNAME (URI::PATTERN::HOSTNAME in default)

@@ -226,6 +227,10 @@ module URI  
end.force\_encoding(Encoding::US\_ASCII)  
end

- def escape\_component(str, unsafe = @regexp[:NON\_UNRESERVED])
- 

```
 escape(str, unsafe)

```

- end
- def unescape(str, escaped = @regexp[:ESCAPED])  
str.gsub(escaped) { [$&[1, 2].hex].pack(‘C’)  
}.force\_encoding(str.encoding)  
end  
@@ -408,6 +413,7 @@ module URI  
# for URI::escape/unescape  
ret[:ESCAPED] = Regexp.new(pattern[:ESCAPED])  
ret[:UNSAFE] =  
Regexp.new(“[^#{pattern[:UNRESERVED]}#{pattern[:RESERVED]}]”)
- 

```
 ret[:NON_UNRESERVED] = Regexp.new("[^#{pattern[:UNRESERVED]}]")

 # for Generic#initialize
 ret[:SCHEME] = Regexp.new("^#{pattern[:SCHEME]}$")

```

@@ -476,10 +482,10 @@ module URI  
# == Args  
#  
# +str+::

- 
# String to replaces in.

- 
# String to escape.

# +unsafe+::

# Regexp that matches all symbols that must be replaced with

codes.

- 
# By default uses REGEXP::UNSAFE.

- 
# By default uses URI::UNSAFE.

# When this argument is a String, it represents a character set.

# 

# == Description

@@ -529,6 +535,36 @@ module URI  
DEFAULT\_PARSER.unescape(\*arg)  
end  
alias decode unescape

- 
# 
- 
# == Synopsis
- 
# 
- 
# URI.escape\_component(str)
- 
# 
- 
# == Args
- 
# 
- 
# +str+::
- 
# String to escape.
- 
# +unsafe+::
- 
# Regexp that matches all symbols that must be replaced with

codes.

- 
# By default uses URI::NON\_UNRESERVED.
- 
# When this argument is a String, it represents a character set.
- 
# 
- 
# == Description
- 
# 
- 
# This function is a version of escape() to escape a string for use
- 
# in URI query components.
- 
# 
- 
# == Usage
- 
# 
- 
# require ‘uri’
- 
# 
- 
# uri = URI.parse(“[http://example.com/plot?q=](http://example.com/plot?q=)” +

URI.escape\_component(“y=2x”))

- 
# p uri
- 
# # =\> “[http://example.com/plot?q=y%3D2x](http://example.com/plot?q=y%3D2x)”
- 
# 
- def escape\_component(\*arg)
- 

```
 DEFAULT_PARSER.escape_component(*arg)

```

- end  
end

# extend Escape Index: test/uri/test\_common.rb

— test/uri/test\_common.rb (revision 24684)  
+++ test/uri/test\_common.rb (working copy)  
@@ -49,6 +49,40 @@ class TestCommon \< Test::Unit::TestCase  
assert\_equal(expected, Kernel::URI(“[http://www.ruby-lang.org/](http://www.ruby-lang.org/)”))  
assert\_raise(NoMethodError) {  
Object.new.URI(“[http://www.ruby-lang.org/](http://www.ruby-lang.org/)”) }  
end  
+

- RESERVED = /\A[;/?:@&=+$,[]]\z/
- UNRESERVED = /\A[-\_.!~\*'()A-Za-z0-9]\z/
- 
- def test\_escape
- unescaped = ‘’
- escaped = ‘’
- escaped\_component = ‘’
- 
- (0x00…0x7f).map { |c|
- 

```
 ch = c.chr

```

- 

```
 esc = '%%%02X' % c

```

- 
- 

```
 unescaped << ch

```

- 
- 

```
 case ch

```

- 

```
 when UNRESERVED

```

- 

```
   escaped << ch

```

- 

```
   escaped_component << ch

```

- 

```
 when RESERVED

```

- 

```
   escaped << ch

```

- 

```
   escaped_component << esc

```

- 

```
 else

```

- 

```
   escaped << esc

```

- 

```
   escaped_component << esc

```

- 

```
 end

```

- }
- 
- assert\_equal(escaped, URI.escape(unescaped))
- assert\_equal(escaped\_component, URI.escape\_component(unescaped))
- 
- assert\_equal(unescaped, URI.unescape(escaped))
- assert\_equal(unescaped, URI.unescape(escaped\_component))
- end  
end

---

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [August 28, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/uri-escape-component/174550/2 "2009-08-28T15:35:19Z")

</div>

e$B@.@%$G$9!#e(B

Akinori MUSHA wrote:

> e$B!!e(BJavaScript e$B$Ne(B escapeURIComponent() e$BAjEv$N4X?t$C$F$"$j$^$7$?$C$1!)e(B  
> e$B$b$7$J$1$l$Pe(BURIe$B%b%8%e!\<%k$KDI2C$9$k$N$O$I$&$G$9$+!#e(B

encodeURIComponent e$B$G$O$"$j$^$;$s$+!)e(B

e$B$5$F$\*$-!"e(BURI.escapee$B$OFHFC$NF0$-$r$9$k$N$G!"e(BJavaScript/ECMAScript  
e$B$Ne(B  
escape e$B$H$be(B encodeURI e$B$H$be(B encodeURIComponent  
e$B$H$b0c$$$^$9!#e(B

e$B$=$&$$$($P!"8=:\_$N;EMM$N0U?^$r$\*J9$-$7$?$+$C$?$s$@$C$?e(B  
[ruby-dev:38124]

e$B$J$\*!“LLE]$JOC$,=P$F$-$?;~$K$O!”$H$j$“$($:e(B akr e$B$5$s$,e(B  
e$B4{$K\<h$j\>e$2$F$$$J$$$+D4$Y$k$o$1$G$9$,!”$d$C$Q$j07$C$F$$$k$o$1$G$9!#e(B  
[http://www.a-k-r.org/d/d2004\_07.html](http://www.a-k-r.org/d/d2004_07.html)  
[https://www.codeblog.org/blog/akr/20070222.html](https://www.codeblog.org/blog/akr/20070222.html)

---

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [September 2, 2009, 9:29am UTC](https://www.ruby-forum.com/t/uri-escape-component/174550/3 "2009-09-02T09:29:31Z")

</div>

(2009e$BG/e(B08e$B7ne(B28e$BF|e(B 21:09), Akinori MUSHA  
e$B$5$s$O=q$-$^$7$?e(B:

> e$B!!e(BJavaScript e$B$Ne(B escapeURIComponent() e$BAjEv$N4X?t$C$F$"$j$^$7$?$C$1!)e(B  
> e$B$b$7$J$1$l$Pe(BURIe$B%b%8%e!\<%k$KDI2C$9$k$N$O$I$&$G$9$+!#e(B

e$B;d$H$7$F$OFCJL$K0U8+$O$J$$$G$9!#e(B  
e$BB\>$K0U8+$,$J$1$l$PDI2C$7$F$7$^$C$F9=$o$J$$$H;W$$$^$9!#e(B

# escapee$B7O$OJL$K$^$H$^$C$F$k$H;H$$0W$$$\_$?$$$JOC$,e(B

# e$B$I$3$+$G=P$F$$$?$N$G$O$J$+$C$?$+$J$H;W$$$^$9!#e(B

# e$B$=$&$J$C$F$k$H$$$$$J!\<$H$O;W$$$^$9!#e(B

---

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [August 28, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/uri-escape-component/174550/4 "2009-08-28T18:23:58Z")

</div>

At Fri, 28 Aug 2009 22:34:46 +0900,  
NARUSE, Yui wrote:

> Akinori MUSHA wrote:
> 
> > ã€€JavaScript ã® escapeURIComponent() ç›¸å½“ã®é–¢æ•°ã£ã¦ã‚ã‚Šã¾ã—ãŸã£ã‘ï¼Ÿ  
> > ã‚‚ã—ãªã‘ã‚Œã°URIãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã«è¿½åŠ ã™ã‚‹ã®ã¯ã©ã†ã§ã™ã‹ã€‚
> 
> encodeURIComponent ã§ã¯ã‚ã‚Šã¾ã›ã‚“ã‹ï¼Ÿ

ã€€ãã†ã§ã—ãŸã€‚

> ã•ã¦ãŠãã€URI.escapeã¯ç‹¬ç‰¹ã®å‹•ãã‚’ã™ã‚‹ã®ã§ã€JavaScript/ECMAScript ã®  
> escape ã¨ã‚‚ encodeURI ã¨ã‚‚ encodeURIComponent ã¨ã‚‚é•ã„ã¾ã™ã€‚
> 
> ãã†ã„ãˆã°ã€ç¾åœ¨ã®ä»•æ§˜ã®æ„å›³ã‚’ãŠèžãã—ãŸã‹ã£ãŸã‚“ã ã£ãŸ  
> [ruby-dev:38124]

ã€€ç§ã‚‚çŸ¥ã‚ŠãŸã„ã¨æ€ã„ã¾ã™ã€‚ãã—ã¦ã€HTTP URIãƒ‘ãƒ©ãƒ¡ãƒ¼ã‚¿ã‚’ç¾å®Ÿä¸–ç•Œã§  
æ‰±ã†éš›ã«å…¸åž‹çš„ã«å¿…è¦ãªæ©Ÿèƒ½ã¯URIãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ãŒå‚™ãˆã¦ã„ã¦ã»ã—ã„ã€‚

ã€€CGIã‚„WEBrickã«ã‚ã£ã¦ã‚‚ãƒ¬ã‚¤ãƒ¤ãŒé•ã†ã®ã§å¤–ã‹ã‚‰å‘¼ã³å‡ºãã†ã¨ã¯æ€ã‚  
ãªã„ã—ã€ãã“ã¯ãã£ã¡ã‚Šæ‹¾ã„ä¸Šã’ãŸã„ã¨ã“ã‚ã§ã™ã€‚

> ãªãŠã€é¢å€’ãªè©±ãŒå‡ºã¦ããŸæ™‚ã«ã¯ã€ã¨ã‚Šã‚ãˆãš akr ã•ã‚“ãŒ  
> æ—¢ã«å–ã‚Šä¸Šã’ã¦ã„ãªã„ã‹èª¿ã¹ã‚‹ã‚ã‘ã§ã™ãŒã€ã‚„ã£ã±ã‚Šæ‰±ã£ã¦ã„ã‚‹ã‚ã‘ã§ã™ã€‚  
> [天泣記](http://www.a-k-r.org/d/d2004_07.html)  
> [https://www.codeblog.org/blog/akr/20070222.html](https://www.codeblog.org/blog/akr/20070222.html)

ã€€æœªã ã« encodeURIComponent() ã™ã‚‰ãªã„ã®ã§ãã®å…ˆã«é€²ã‚ãªã„ã‚“ã§ã™  
ã‚ˆã­ã€‚æ‰‹å…ƒã§ã¯ Perl ã® URI::QueryParam ç›¸å½“ã‚’ä½œã£ã¦ã„ã‚‹ã‚“ã§ã™ãŒã€  
URI ã¨ã®é–¢ä¿‚ã‚’ã©ã†ã—ã‚ˆã†ã‹æ‚©ã‚“ã§ã„ã‚‹ã¨ã“ã‚ã§ã™ã€‚ã¨ã‚Šã‚ãˆãš URI ã®  
æ–¹é‡ã‚’æ˜Žã‚‰ã‹ã«ã—ã¦ã„ãã¨ã“ã‚ã‹ã‚‰ã§ã—ã‚‡ã†ã‹ã€‚

---

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [September 2, 2009, 10:38am UTC](https://www.ruby-forum.com/t/uri-escape-component/174550/5 "2009-09-02T10:38:36Z")

</div>

e$B@.@%$G$9!#e(B

2009/09/02 16:28, akira yamada / e$B$d$^$@$"$-$ie(B wrote:

> (2009e$BG/e(B08e$B7ne(B28e$BF|e(B 21:09), Akinori MUSHA e$B$5$s$O=q$-$^$7$?e(B:
> 
> > e$B!!e(BJavaScript e$B$Ne(B escapeURIComponent() e$BAjEv$N4X?t$C$F$"$j$^$7$?$C$1!)e(B  
> > e$B$b$7$J$1$l$Pe(BURIe$B%b%8%e!\<%k$KDI2C$9$k$N$O$I$&$G$9$+!#e(B
> 
> e$B;d$H$7$F$OFCJL$K0U8+$O$J$$$G$9!#e(B  
> e$BB\>$K0U8+$,$J$1$l$PDI2C$7$F$7$^$C$F9=$o$J$$$H;W$$$^$9!#e(B

e$BA02s$O%Q%C%A$^$G8+$F$J$+$C$?$s$G$9$,!"e(B  
URI.escape\_component e$B$N0z?t$Oe(B str  
e$B$N$\_$NJ}$,$$$$$N$G$O$J$$$G$7$g$&$+!#e(B  
e$BCV49%Q%?!\<%s$rM?$($i$l$kI,MW$OL5$$$h$&$K;W$($^$9!#e(B  
e$B\<BAu$be(B String#gsub(pattern, hash) e$B$r;H$C$?J}$,B.$$$G$9$+$M!#e(B

> # escapee$B7O$OJL$K$^$H$^$C$F$k$H;H$$0W$$$\_$?$$$JOC$,e(B
> 
> # e$B$I$3$+$G=P$F$$$?$N$G$O$J$+$C$?$+$J$H;W$$$^$9!#e(B
> 
> # e$B$=$&$J$C$F$k$H$$$$$J!\<$H$O;W$$$^$9!#e(B

e$B0JA05DO@$5$l$?$s$G$9$,!"7k6Ie(B String#gsub(pattern, hash)  
e$B$H$$$&7kO@$G$7$?$M!#e(B  
e$B$^$!!"e(BURI e$B7O$Oe(B URI e$B$G$$$$5$$,$7$^$9!#e(B
