1108. Defanging an IP Address
Description
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
Example 1:
Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1"
Example 2:
Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0"
Constraints:
- The given
address
is a valid IPv4 address.
My Solution
Source Code
1
2
3
4
5
6
7
/**
* @param {string} address
* @return {string}
*/
let defangIPaddr = function(address) {
return address.replace(/\./g, "[.]");
};
Analysis
Okay maybe this problem was a little too easy. All I did was use a simple regular expression. Using JavaScript's built-in functions is almost always going to be faster than implementing your own string replacement functions so I didn't see a need to do that. Plus it's always good to get more practice with regular expressions.