IPv6 入门教程:正则表达式

2022-03-22 15:50:38 浏览数 (1)

  • 作者:约克
  • 原文地址:https://yorkyu.cn/ipv6-getting-started-regexp-ffe6f1d758c1.html
  • 文章版权归作者所有,转载请注明出处!

前言

在前端开发中,处理表单时,常遇到对 IPv6/Ipv4 地址合法性校验的场景。

一,IPv6 正则

1.1. 万能正则

斯蒂芬·瑞恩(Stephen Ryan)写了一个非常有用的正则表达式,可用于匹配任何一个合法的IPv6地址。以下为正则表达式的代码

代码语言:javascript复制
const ipv6Regexp = /^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%. )?s*$/;

1.2. 匹配示列

代码语言:javascript复制
// full form of IPv6 
ipv6Regexp.test('fe80:0000:0000:0000:0204:61ff:fe9d:f156');
=> true

// drop leading zeroes
ipv6Regexp.test('fe80:0:0:0:204:61ff:fe9d:f156');
=> true

// collapse multiple zeroes to :: in the IPv6 address
ipv6Regexp.test('fe80::204:61ff:fe9d:f156');
=> true

// IPv4 dotted quad at the end 
ipv6Regexp.test('fe80:0000:0000:0000:0204:61ff:254.157.241.86');
=> true

// drop leading zeroes, IPv4 dotted quad at the end 
ipv6Regexp.test('fe80:0:0:0:0204:61ff:254.157.241.86');
=> true

// dotted quad at the end, multiple zeroes collapsed 
ipv6Regexp.test('fe80::204:61ff:254.157.241.86');
=> true

// localhost
ipv6Regexp.test('::1');
=> true

// link-local prefix 
ipv6Regexp.test('fe80::');
=> true

// global unicast prefix 
ipv6Regexp.test('2001::');
=> true

二,CIDR 正则

CIDR 表示法匹配IP地址的正则表达式。

2.1. 用法

代码语言:javascript复制
$ npm i -S cidr-regex
代码语言:javascript复制
const cidrRegex = require('cidr-regex');

// Contains a CIDR IP address?
cidrRegex().test('foo 192.168.0.1/24');
=> true

// Is a CIDR IP address?
cidrRegex({exact: true}).test('foo 192.168.0.1/24');
=> false

cidrRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8/64');
=> true

// Extract CIDRs from string
'foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz'.match(cidrRegex());
=> ['192.168.0.1/24', '1:2:3:4:5:6:7:8/64']

三,NPM 包

  • is-ip - Check if a string is an IP address
  • is-cidr - Check if a string is an IP address in CIDR notation
  • cidr-regex - Regular expression for matching IP addresses in CIDR notation

四,正则可视化工具

使用正则可视化工具,便捷/快速分析表达式结构

4.1. Regulex

Regulex 是开源的正则可视化工具,实时展示可视化效果,方便正则调试,可视化美观等。点击查看

参考

  • [1] A Regular Expression for IPv6 Addresses
  • [2] IPv6正则表达式
  • [3] cidr-regex

0 人点赞