nice-people-frontend-community / nice-js-leetcode

好青年 | leetcode 今日事今日毕(✅ Solutions to LeetCode by JavaScript, 100% test coverage, runtime beats 100% / LeetCode 题解 / GitHub Actions集成LeetCode每日一题至issues)

Home Page:https://nice-people-frontend-community.github.io/nice-js-leetcode/

Repository from Github https://github.comnice-people-frontend-community/nice-js-leetcodeRepository from Github https://github.comnice-people-frontend-community/nice-js-leetcode

[2022-06-21]1108. IP 地址无效化👋字符串

webVueBlog opened this issue · comments

1108. IP 地址无效化

Description

Difficulty: 简单

Related Topics: 字符串

给你一个有效的 地址 address,返回这个 IP 地址的无效化版本。

所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."

示例 1:

输入:address = "1.1.1.1"
输出:"1[.]1[.]1[.]1"

示例 2:

输入:address = "255.100.50.0"
输出:"255[.]100[.]50[.]0"

提示:

  • 给出的 address 是一个有效的 IPv4 地址

Solution

Language: JavaScript

/**
 * @param {string} address
 * @return {string}
 */
var defangIPaddr = function(address) {
    return address.replace(/[.]/g, '[.]');
};