Skip to content

Commit

Permalink
feat: 新增 IP 地址合法性验证题目
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Nov 19, 2022
1 parent f79ffd3 commit 6ac746f
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
11 changes: 11 additions & 0 deletions question/FrontEnd/ipAddress/answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### 方法一

正则表达式

```js
export default function isIp(ip) {
return /^((2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})$/.test(
ip
);
}
```
5 changes: 5 additions & 0 deletions question/FrontEnd/ipAddress/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function f(ip) {
return /((2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})/.test(
ip
);
}
59 changes: 59 additions & 0 deletions question/FrontEnd/ipAddress/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
验证 IP 地址合法性

一个合法的 IP 地址 在 0.0.0.0 ~ 255.255.255.255 的范围

具体的 IP 地址 分类

- A 类 IP 地址 范围 0.0.0.0 ~ 127.255.255.255
- B 类 IP 地址 范围 128.0.0.0 ~ 191.255.255.255
- C 类 IP 地址 范围 192.0.0.0 ~ 223.255.255.255
- D 类 Ip 地址 范围 224.0.0.0 ~ 239.255.255.255
- E 类 IP 地址 范围 240.0.0.0 ~ 255.255.255.255

用例 1:

```js
const ip = '192.168.1.1';

isIp(ip); // true
```

用例 2:

```js
const ip = '127.0.0.1';

isIp(ip); // true
```

用例 3:

```js
const ip = '254.254.254.256';

isIp(ip); // false
```

用例 4:

```js
const ip = '255.255.255.255';

isIp(ip); // true
```

用例 5:

```js
const ip = '0.0.0.0';

isIp(ip); // true
```

用例 6:

```js
const ip = '192.168.1.-1';

isIp(ip); // false
```
12 changes: 12 additions & 0 deletions question/FrontEnd/ipAddress/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 判断 IP 地址是否合法
*
* @export
* @param {string} ip
* @return {boolean}
*/
export default function isIp(ip) {
return /^((2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})$/.test(
ip
);
}
31 changes: 31 additions & 0 deletions question/FrontEnd/ipAddress/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import f from './index.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

it('用例 1: 输入 127.0.0.1', () => {
assert.deepEqual(f('127.0.0.1'), true);
});

it('用例 2: 输入 192.168.1.1', () => {
assert.deepEqual(f('192.168.1.1'), true);
});

it('用例 3: 输入 0.0.0.0', () => {
assert.deepEqual(f('0.0.0.0'), true);
});

it('用例 4: 输入 254.254.254.256', () => {
assert.deepEqual(f('254.254.254.256'), false);
});

it('用例 5: 输入 255.255.255.255', () => {
assert.deepEqual(f('255.255.255.255'), true);
});

it('用例 6: 输入 192.168.1.-1', () => {
assert.deepEqual(f('192.168.1.-1'), false);
});

it('用例 7: 输入 192.168.1.260', () => {
assert.deepEqual(f('192.168.1.260'), false);
});
6 changes: 6 additions & 0 deletions question/FrontEnd/template/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@
```js

```

用例 3:

```js

```

0 comments on commit 6ac746f

Please sign in to comment.