Created
June 30, 2020 19:24
-
-
Save andreiskandar/9965281e1df7c0f2424964049da9ecb3 to your computer and use it in GitHub Desktop.
Replacing all whitespaces with '%20' from given string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kata 5 - Percent Encoded String | |
In this exercise, we will be given a normal string of words and turn it into a percent-encoded string by replacing all whitespace with %20. | |
Percent Encoding | |
Take a look at the following URL, specifically the last part: | |
This URL will perform a google search for the term "lighthouse labs". Notice that when the string "lighthouse labs" is part of a URL, the space is replaced with %20. | |
If we want to add a parameter to a url, there are certain characters that must be encoded in order to make the URL valid. There are many characters that must be encoded, including: , !, ", and #. For this exercise, we will only be focusing on replacing the space with %20. | |
Input | |
const urlEncode = function(text) { | |
// Put your solution here | |
}; | |
console.log(urlEncode("Lighthouse Labs")); | |
console.log(urlEncode(" Lighthouse Labs ")); | |
console.log(urlEncode("blue is greener than purple for sure")); | |
Expected Output | |
Lighthouse%20Labs | |
Lighthouse%20Labs | |
blue%20is%20greener%20than%20purple%20for%20sure | |
*/ | |
const urlEncode = function(text) { | |
const regex = /\s+/g; | |
return text.replace(regex, '%20'); | |
}; | |
console.log(urlEncode("blue is greener than purple for sure")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment