URL Encoder/Decoder

Encode and decode URL components


1. What is URL Encoding?

URL encoding converts characters into a format that can be transmitted over the Internet. Special characters are replaced with a '%' followed by two hexadecimal digits.

2. How does it work?

Use URL encoding when you need to include special characters in URL query parameters or when building URLs programmatically. Common use cases include encoding spaces, special characters, and non-ASCII characters.

3. Examples

Space encoding

Hello World → Hello%20World

Special characters

[email protected] → user%40example.com

Query parameters

name=John Doe&age=25 → name%3DJohn%20Doe%26age%3D25

4. Source Code

typescript
1// Encode URL component (replaces special characters with percent-encoded values)
2function encodeUrl(text: string): string {
3  return encodeURIComponent(text);
4}
5
6// Decode URL component
7function decodeUrl(encodedText: string): string {
8  return decodeURIComponent(encodedText);
9}
10