title: How to escape URL but keep the square brackets around IPv6 address unchanged
body: <p>I have a URL which I need to percent-encode using Java or Javascript.</p>
  
  <p>I can't know in advance if the URL will or will not include protocol, port or path. Also I can't know if the host will be a name, IPv4 address or IPv6 address.</p>
  
  <p>This URL:</p>
  
  <pre><code>  http://[fc00::90:90]/path/to/[file].txt
  </code></pre>
  
  <p>has two sets of square brackets. The first is ok, needed for the IPv6 address. But the second should be percent-escaped. So the URL after escaping should be:</p>
  
  <pre><code>http://[fc00::90:90]/path/to/%5Bfile%5D.txt
  </code></pre>
  
  <p>How can I escape the second pair but not the first pair?</p>
  
  <p>I thought <code>URI.toASCIIString</code> is the obvious solution, but it doesn't like the brackets in the file name and throws </p>
  
  <p><code>java.net.URISyntaxException: Illegal character in path at index 29: http://[fc00::90:90]/path/to/[file].txt</code></p>
  
  <p>There is also an option to parse the URL into its components and escape only the path part, but I prefer not to do that if that's possible.</p>
  
  <p>Can anyone suggest a solution using Java or Javascript?
  Thanks.</p>