├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/ip-regex 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. *(`false` matches any IP address in a string)* 4 | 5 | @default false 6 | */ 7 | readonly exact?: boolean; 8 | 9 | /** 10 | Include boundaries in the regex. When `true`, `192.168.0.2000000000` will report as an invalid IPv4 address. If this option is not set, the mentioned IPv4 address would report as valid (ignoring the trailing zeros). 11 | 12 | @default false 13 | */ 14 | readonly includeBoundaries?: boolean; 15 | } 16 | 17 | declare const ipRegex: { 18 | /** 19 | Regular expression for matching IP addresses. 20 | 21 | @returns A regex for matching both IPv4 and IPv6. 22 | 23 | @example 24 | ``` 25 | import ipRegex from 'ip-regex'; 26 | 27 | // Contains an IP address? 28 | ipRegex().test('unicorn 192.168.0.1'); 29 | //=> true 30 | 31 | // Is an IP address? 32 | ipRegex({exact: true}).test('unicorn 192.168.0.1'); 33 | //=> false 34 | 35 | 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex()); 36 | //=> ['192.168.0.1', '1:2:3:4:5:6:7:8'] 37 | 38 | // Contains an IP address? 39 | ipRegex({includeBoundaries: true}).test('192.168.0.2000000000'); 40 | //=> false 41 | 42 | // Matches an IP address? 43 | '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true})); 44 | //=> null 45 | ``` 46 | */ 47 | (options?: Options): RegExp; 48 | 49 | /** 50 | @returns A regex for matching IPv4. 51 | */ 52 | v4(options?: Options): RegExp; 53 | 54 | /** 55 | @returns A regex for matching IPv6. 56 | 57 | @example 58 | ``` 59 | import ipRegex from 'ip-regex'; 60 | 61 | ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8'); 62 | //=> true 63 | ``` 64 | */ 65 | v6(options?: Options): RegExp; 66 | }; 67 | 68 | export default ipRegex; 69 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const word = '[a-fA-F\\d:]'; 2 | 3 | const boundry = options => options && options.includeBoundaries 4 | ? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))` 5 | : ''; 6 | 7 | const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; 8 | 9 | const v6segment = '[a-fA-F\\d]{1,4}'; 10 | 11 | const v6 = ` 12 | (?: 13 | (?:${v6segment}:){7}(?:${v6segment}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 14 | (?:${v6segment}:){6}(?:${v4}|:${v6segment}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4 15 | (?:${v6segment}:){5}(?::${v4}|(?::${v6segment}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4 16 | (?:${v6segment}:){4}(?:(?::${v6segment}){0,1}:${v4}|(?::${v6segment}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4 17 | (?:${v6segment}:){3}(?:(?::${v6segment}){0,2}:${v4}|(?::${v6segment}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4 18 | (?:${v6segment}:){2}(?:(?::${v6segment}){0,3}:${v4}|(?::${v6segment}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4 19 | (?:${v6segment}:){1}(?:(?::${v6segment}){0,4}:${v4}|(?::${v6segment}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4 20 | (?::(?:(?::${v6segment}){0,5}:${v4}|(?::${v6segment}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4 21 | )(?:%[0-9a-zA-Z]{1,})? // %eth0 %1 22 | `.replace(/\s*\/\/.*$/gm, '').replace(/\n/g, '').trim(); 23 | 24 | // Pre-compile only the exact regexes because adding a global flag make regexes stateful 25 | const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`); 26 | const v4exact = new RegExp(`^${v4}$`); 27 | const v6exact = new RegExp(`^${v6}$`); 28 | 29 | const ipRegex = options => options && options.exact 30 | ? v46Exact 31 | : new RegExp(`(?:${boundry(options)}${v4}${boundry(options)})|(?:${boundry(options)}${v6}${boundry(options)})`, 'g'); 32 | 33 | ipRegex.v4 = options => options && options.exact ? v4exact : new RegExp(`${boundry(options)}${v4}${boundry(options)}`, 'g'); 34 | ipRegex.v6 = options => options && options.exact ? v6exact : new RegExp(`${boundry(options)}${v6}${boundry(options)}`, 'g'); 35 | 36 | export default ipRegex; 37 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import ipRegex from './index.js'; 3 | 4 | expectType(ipRegex()); 5 | expectType(ipRegex({exact: true})); 6 | expectType(ipRegex({includeBoundaries: true})); 7 | expectType(ipRegex.v4()); 8 | expectType(ipRegex.v4({exact: true})); 9 | expectType(ipRegex.v4({includeBoundaries: true})); 10 | expectType(ipRegex.v6()); 11 | expectType(ipRegex.v6({exact: true})); 12 | expectType(ipRegex.v6({includeBoundaries: true})); 13 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ip-regex", 3 | "version": "5.0.0", 4 | "description": "Regular expression for matching IP addresses (IPv4 & IPv6)", 5 | "license": "MIT", 6 | "repository": "sindresorhus/ip-regex", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "ip", 27 | "ipv6", 28 | "ipv4", 29 | "regex", 30 | "regexp", 31 | "re", 32 | "match", 33 | "test", 34 | "find", 35 | "text", 36 | "pattern", 37 | "internet", 38 | "protocol", 39 | "address", 40 | "validate" 41 | ], 42 | "devDependencies": { 43 | "ava": "^3.15.0", 44 | "tsd": "^0.19.1", 45 | "xo": "^0.47.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ip-regex 2 | 3 | > Regular expression for matching IP addresses 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install ip-regex 9 | ``` 10 | 11 | This module targets Node.js 12 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, use version 2.1.0: `npm install ip-regex@2.1.0` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import ipRegex from 'ip-regex'; 17 | 18 | // Contains an IP address? 19 | ipRegex().test('unicorn 192.168.0.1'); 20 | //=> true 21 | 22 | // Is an IP address? 23 | ipRegex({exact: true}).test('unicorn 192.168.0.1'); 24 | //=> false 25 | 26 | ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8'); 27 | //=> true 28 | 29 | 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex()); 30 | //=> ['192.168.0.1', '1:2:3:4:5:6:7:8'] 31 | 32 | // Contains an IP address? 33 | ipRegex({includeBoundaries: true}).test('192.168.0.2000000000'); 34 | //=> false 35 | 36 | // Matches an IP address? 37 | '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true})); 38 | //=> null 39 | ``` 40 | 41 | ## API 42 | 43 | ### ipRegex(options?) 44 | 45 | Returns a regex for matching both IPv4 and IPv6. 46 | 47 | ### ipRegex.v4(options?) 48 | 49 | Returns a regex for matching IPv4. 50 | 51 | ### ipRegex.v6(options?) 52 | 53 | Returns a regex for matching IPv6. 54 | 55 | #### options 56 | 57 | Type: `object` 58 | 59 | ##### exact 60 | 61 | Type: `boolean`\ 62 | Default: `false` *(Matches any IP address in a string)* 63 | 64 | Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. 65 | 66 | ##### includeBoundaries 67 | 68 | Type: `boolean`\ 69 | Default: `false` 70 | 71 | Include boundaries in the regex. When `true`, `192.168.0.2000000000` will report as an invalid IPv4 address. If this option is not set, the mentioned IPv4 address would report as valid (ignoring the trailing zeros). 72 | 73 | ## Important 74 | 75 | If you run the regex against untrusted user input in a server context, you should [give it a timeout](https://github.com/sindresorhus/super-regex). 76 | 77 | **I do not consider ReDoS a valid vulnerability for this package. It's simply not possible to make it fully ReDoS safe. It's up to the user to set a timeout for the regex if they accept untrusted user input.** However, I'm happy to accept pull requests to improve the regex. 78 | 79 | ## Related 80 | 81 | - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address 82 | - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation 83 | - [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation 84 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import ipRegex from './index.js'; 3 | 4 | const v4 = [ 5 | '0.0.0.0', 6 | '8.8.8.8', 7 | '127.0.0.1', 8 | '100.100.100.100', 9 | '192.168.0.1', 10 | '18.101.25.153', 11 | '123.23.34.2', 12 | '172.26.168.134', 13 | '212.58.241.131', 14 | '128.0.0.0', 15 | '23.71.254.72', 16 | '223.255.255.255', 17 | '192.0.2.235', 18 | '99.198.122.146', 19 | '46.51.197.88', 20 | '173.194.34.134', 21 | ]; 22 | 23 | const v4not = [ 24 | '.100.100.100.100', 25 | '100..100.100.100.', 26 | '100.100.100.100.', 27 | '999.999.999.999', 28 | '256.256.256.256', 29 | '256.100.100.100.100', 30 | '123.123.123', 31 | 'http://123.123.123', 32 | '1000.2.3.4', 33 | '999.2.3.4', 34 | ]; 35 | 36 | const v4boundaries = { 37 | '0000000192.168.0.200': ['192.168.0.200'], 38 | '192.168.0.2000000000': ['192.168.0.200'], 39 | }; 40 | 41 | const v4extract = { 42 | '255.255.255.255 0.0.0.0': ['255.255.255.255', '0.0.0.0'], 43 | '1.2.3.4, 6.7.8.9, 1.2.3.4-5.6.7.8': ['1.2.3.4', '6.7.8.9', '1.2.3.4', '5.6.7.8'], 44 | '1.2.3.4 6.7.8.9 1.2.3.4 - 5.6.7.8': ['1.2.3.4', '6.7.8.9', '1.2.3.4', '5.6.7.8'], 45 | '192.168.0.2000000000': ['192.168.0.200'], 46 | }; 47 | 48 | const v6 = [ 49 | '::', 50 | '1::', 51 | '::1', 52 | '1::8', 53 | '1::7:8', 54 | '1:2:3:4:5:6:7:8', 55 | '1:2:3:4:5:6::8', 56 | '1:2:3:4:5:6:7::', 57 | '1:2:3:4:5::7:8', 58 | '1:2:3:4:5::8', 59 | '1:2:3::8', 60 | '1::4:5:6:7:8', 61 | '1::6:7:8', 62 | '1::3:4:5:6:7:8', 63 | '1:2:3:4::6:7:8', 64 | '1:2::4:5:6:7:8', 65 | '::2:3:4:5:6:7:8', 66 | '1:2::8', 67 | '2001:0000:1234:0000:0000:C1C0:ABCD:0876', 68 | '3ffe:0b00:0000:0000:0001:0000:0000:000a', 69 | 'FF02:0000:0000:0000:0000:0000:0000:0001', 70 | '0000:0000:0000:0000:0000:0000:0000:0001', 71 | '0000:0000:0000:0000:0000:0000:0000:0000', 72 | '::ffff:192.168.1.26', 73 | '2::10', 74 | 'ff02::1', 75 | 'fe80::', 76 | '2002::', 77 | '2001:db8::', 78 | '2001:0db8:1234::', 79 | '::ffff:0:0', 80 | '::ffff:192.168.1.1', 81 | '1:2:3:4::8', 82 | '1::2:3:4:5:6:7', 83 | '1::2:3:4:5:6', 84 | '1::2:3:4:5', 85 | '1::2:3:4', 86 | '1::2:3', 87 | '::2:3:4:5:6:7', 88 | '::2:3:4:5:6', 89 | '::2:3:4:5', 90 | '::2:3:4', 91 | '::2:3', 92 | '::8', 93 | '1:2:3:4:5:6::', 94 | '1:2:3:4:5::', 95 | '1:2:3:4::', 96 | '1:2:3::', 97 | '1:2::', 98 | '1:2:3:4::7:8', 99 | '1:2:3::7:8', 100 | '1:2::7:8', 101 | '1:2:3:4:5:6:1.2.3.4', 102 | '1:2:3:4:5::1.2.3.4', 103 | '1:2:3:4::1.2.3.4', 104 | '1:2:3::1.2.3.4', 105 | '1:2::1.2.3.4', 106 | '1::1.2.3.4', 107 | '1:2:3:4::5:1.2.3.4', 108 | '1:2:3::5:1.2.3.4', 109 | '1:2::5:1.2.3.4', 110 | '1::5:1.2.3.4', 111 | '1::5:11.22.33.44', 112 | 'fe80::217:f2ff:254.7.237.98', 113 | 'fe80::217:f2ff:fe07:ed62', 114 | '2001:DB8:0:0:8:800:200C:417A', 115 | 'FF01:0:0:0:0:0:0:101', 116 | '0:0:0:0:0:0:0:1', 117 | '0:0:0:0:0:0:0:0', 118 | '2001:DB8::8:800:200C:417A', 119 | 'FF01::101', 120 | '0:0:0:0:0:0:13.1.68.3', 121 | '0:0:0:0:0:FFFF:129.144.52.38', 122 | '::13.1.68.3', 123 | '::FFFF:129.144.52.38', 124 | 'fe80:0000:0000:0000:0204:61ff:fe9d:f156', 125 | 'fe80:0:0:0:204:61ff:fe9d:f156', 126 | 'fe80::204:61ff:fe9d:f156', 127 | 'fe80:0:0:0:204:61ff:254.157.241.86', 128 | 'fe80::204:61ff:254.157.241.86', 129 | 'fe80::1', 130 | '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 131 | '2001:db8:85a3:0:0:8a2e:370:7334', 132 | '2001:db8:85a3::8a2e:370:7334', 133 | '2001:0db8:0000:0000:0000:0000:1428:57ab', 134 | '2001:0db8:0000:0000:0000::1428:57ab', 135 | '2001:0db8:0:0:0:0:1428:57ab', 136 | '2001:0db8:0:0::1428:57ab', 137 | '2001:0db8::1428:57ab', 138 | '2001:db8::1428:57ab', 139 | '::ffff:12.34.56.78', 140 | '::ffff:0c22:384e', 141 | '2001:0db8:1234:0000:0000:0000:0000:0000', 142 | '2001:0db8:1234:ffff:ffff:ffff:ffff:ffff', 143 | '2001:db8:a::123', 144 | '::ffff:192.0.2.128', 145 | '::ffff:c000:280', 146 | 'a:b:c:d:e:f:f1:f2', 147 | 'a:b:c::d:e:f:f1', 148 | 'a:b:c::d:e:f', 149 | 'a:b:c::d:e', 150 | 'a:b:c::d', 151 | '::a', 152 | '::a:b:c', 153 | '::a:b:c:d:e:f:f1', 154 | 'a::', 155 | 'a:b:c::', 156 | 'a:b:c:d:e:f:f1::', 157 | 'a:bb:ccc:dddd:000e:00f:0f::', 158 | '0:a:0:a:0:0:0:a', 159 | '0:a:0:0:a:0:0:a', 160 | '2001:db8:1:1:1:1:0:0', 161 | '2001:db8:1:1:1:0:0:0', 162 | '2001:db8:1:1:0:0:0:0', 163 | '2001:db8:1:0:0:0:0:0', 164 | '2001:db8:0:0:0:0:0:0', 165 | '2001:0:0:0:0:0:0:0', 166 | 'A:BB:CCC:DDDD:000E:00F:0F::', 167 | '0:0:0:0:0:0:0:a', 168 | '0:0:0:0:a:0:0:0', 169 | '0:0:0:a:0:0:0:0', 170 | 'a:0:0:a:0:0:a:a', 171 | 'a:0:0:a:0:0:0:a', 172 | 'a:0:0:0:a:0:0:a', 173 | 'a:0:0:0:a:0:0:0', 174 | 'a:0:0:0:0:0:0:0', 175 | 'fe80::7:8%eth0', 176 | 'fe80::7:8%1', 177 | ]; 178 | 179 | const v6not = [ 180 | '', 181 | '1:', 182 | ':1', 183 | '11:36:12', 184 | '02001:0000:1234:0000:0000:C1C0:ABCD:0876', 185 | '2001:0000:1234:0000:00001:C1C0:ABCD:0876', 186 | '2001:0000:1234: 0000:0000:C1C0:ABCD:0876', 187 | '2001:1:1:1:1:1:255Z255X255Y255', 188 | '3ffe:0b00:0000:0001:0000:0000:000a', 189 | 'FF02:0000:0000:0000:0000:0000:0000:0000:0001', 190 | '3ffe:b00::1::a', 191 | '::1111:2222:3333:4444:5555:6666::', 192 | '1:2:3::4:5::7:8', 193 | '12345::6:7:8', 194 | '1::5:400.2.3.4', 195 | '1::5:260.2.3.4', 196 | '1::5:256.2.3.4', 197 | '1::5:1.256.3.4', 198 | '1::5:1.2.256.4', 199 | '1::5:1.2.3.256', 200 | '1::5:300.2.3.4', 201 | '1::5:1.300.3.4', 202 | '1::5:1.2.300.4', 203 | '1::5:1.2.3.300', 204 | '1::5:900.2.3.4', 205 | '1::5:1.900.3.4', 206 | '1::5:1.2.900.4', 207 | '1::5:1.2.3.900', 208 | '1::5:300.300.300.300', 209 | '1::5:3000.30.30.30', 210 | '1::400.2.3.4', 211 | '1::260.2.3.4', 212 | '1::256.2.3.4', 213 | '1::1.256.3.4', 214 | '1::1.2.256.4', 215 | '1::1.2.3.256', 216 | '1::300.2.3.4', 217 | '1::1.300.3.4', 218 | '1::1.2.300.4', 219 | '1::1.2.3.300', 220 | '1::900.2.3.4', 221 | '1::1.900.3.4', 222 | '1::1.2.900.4', 223 | '1::1.2.3.900', 224 | '1::300.300.300.300', 225 | '1::3000.30.30.30', 226 | '::400.2.3.4', 227 | '::260.2.3.4', 228 | '::256.2.3.4', 229 | '::1.256.3.4', 230 | '::1.2.256.4', 231 | '::1.2.3.256', 232 | '::300.2.3.4', 233 | '::1.300.3.4', 234 | '::1.2.300.4', 235 | '::1.2.3.300', 236 | '::900.2.3.4', 237 | '::1.900.3.4', 238 | '::1.2.900.4', 239 | '::1.2.3.900', 240 | '::300.300.300.300', 241 | '::3000.30.30.30', 242 | '2001:DB8:0:0:8:800:200C:417A:221', 243 | 'FF01::101::2', 244 | '1111:2222:3333:4444::5555:', 245 | '1111:2222:3333::5555:', 246 | '1111:2222::5555:', 247 | '1111::5555:', 248 | '::5555:', 249 | ':::', 250 | '1111:', 251 | ':', 252 | ':1111:2222:3333:4444::5555', 253 | ':1111:2222:3333::5555', 254 | ':1111:2222::5555', 255 | ':1111::5555', 256 | ':::5555', 257 | '1.2.3.4:1111:2222:3333:4444::5555', 258 | '1.2.3.4:1111:2222:3333::5555', 259 | '1.2.3.4:1111:2222::5555', 260 | '1.2.3.4:1111::5555', 261 | '1.2.3.4::5555', 262 | '1.2.3.4::', 263 | 'fe80:0000:0000:0000:0204:61ff:254.157.241.086', 264 | '123', 265 | 'ldkfj', 266 | '2001::FFD3::57ab', 267 | '2001:db8:85a3::8a2e:37023:7334', 268 | '2001:db8:85a3::8a2e:370k:7334', 269 | '1:2:3:4:5:6:7:8:9', 270 | '1::2::3', 271 | '1:::3:4:5', 272 | '1:2:3::4:5:6:7:8:9', 273 | '::ffff:2.3.4', 274 | '::ffff:257.1.2.3', 275 | '::ffff:12345678901234567890.1.26', 276 | ]; 277 | 278 | const v6boundaries = { 279 | '02001:0000:1234:0000:0000:C1C0:ABCD:0876': ['2001:0000:1234:0000:0000:C1C0:ABCD:0876'], 280 | 'fe80:0000:0000:0000:0204:61ff:fe9d:f156245': ['fe80:0000:0000:0000:0204:61ff:fe9d:f156'], 281 | }; 282 | 283 | const v6extract = { 284 | '::1, ::2, ::3-::5': ['::1', '::2', '::3', '::5'], 285 | '::1 ::2 ::3 - ::5': ['::1', '::2', '::3', '::5'], 286 | '::ffff:192.168.1.1 1::1.2.3.4': ['::ffff:192.168.1.1', '1::1.2.3.4'], 287 | '02001:0000:1234:0000:0000:C1C0:ABCD:0876 a::xyz': ['2001:0000:1234:0000:0000:C1C0:ABCD:0876', 'a::'], 288 | }; 289 | 290 | test('ip', t => { 291 | for (const fixture of v4) { 292 | t.true(ipRegex({exact: true}).test(fixture)); 293 | } 294 | 295 | for (const fixture of v4) { 296 | t.is((ipRegex().exec(`foo ${fixture} bar`) || [])[0], fixture); 297 | } 298 | 299 | for (const fixture of v4) { 300 | t.true(ipRegex().test(`foo${fixture}bar`)); 301 | t.false(ipRegex({includeBoundaries: true}).test(`foo${fixture}bar`)); 302 | } 303 | 304 | for (const fixture of v4not) { 305 | t.false(ipRegex({exact: true}).test(fixture)); 306 | } 307 | 308 | for (const fixture of v6) { 309 | t.true(ipRegex({exact: true}).test(fixture)); 310 | } 311 | 312 | for (const fixture of v6) { 313 | t.is((ipRegex().exec(`foo ${fixture} bar`) || [])[0], fixture); 314 | } 315 | 316 | for (const fixture of v6) { 317 | t.true(ipRegex().test(`foo${fixture}bar`)); 318 | t.false(ipRegex({includeBoundaries: true}).test(`foo${fixture}bar`)); 319 | } 320 | 321 | for (const fixture of v6not) { 322 | t.false(ipRegex({exact: true}).test(fixture)); 323 | } 324 | 325 | for (const fixture of Object.keys(v4boundaries)) { 326 | t.true(ipRegex().test(fixture)); 327 | t.deepEqual(fixture.match(ipRegex()), v4boundaries[fixture]); 328 | t.false(ipRegex({includeBoundaries: true}).test(fixture)); 329 | t.is(fixture.match(ipRegex({includeBoundaries: true})), null); 330 | } 331 | 332 | for (const fixture of Object.keys(v4extract)) { 333 | t.deepEqual(fixture.match(ipRegex()), v4extract[fixture]); 334 | } 335 | 336 | for (const fixture of Object.keys(v6boundaries)) { 337 | t.true(ipRegex().test(fixture)); 338 | t.deepEqual(fixture.match(ipRegex()), v6boundaries[fixture]); 339 | t.false(ipRegex({includeBoundaries: true}).test(fixture)); 340 | t.is(fixture.match(ipRegex({includeBoundaries: true})), null); 341 | } 342 | 343 | for (const fixture of Object.keys(v6extract)) { 344 | t.deepEqual(fixture.match(ipRegex()), v6extract[fixture]); 345 | } 346 | }); 347 | 348 | test('ip v4', t => { 349 | for (const fixture of v4) { 350 | t.true(ipRegex.v4({exact: true}).test(fixture)); 351 | } 352 | 353 | for (const fixture of v4) { 354 | t.is((ipRegex.v4().exec(`foo ${fixture} bar`) || [])[0], fixture); 355 | } 356 | 357 | for (const fixture of v4) { 358 | t.true(ipRegex.v4().test(`foo${fixture}bar`)); 359 | t.false(ipRegex.v4({includeBoundaries: true}).test(`foo${fixture}bar`)); 360 | } 361 | 362 | for (const fixture of v4not) { 363 | t.false(ipRegex.v4({exact: true}).test(fixture)); 364 | } 365 | 366 | for (const fixture of Object.keys(v4boundaries)) { 367 | t.true(ipRegex.v4().test(fixture)); 368 | t.deepEqual(fixture.match(ipRegex.v4()), v4boundaries[fixture]); 369 | t.false(ipRegex.v4({includeBoundaries: true}).test(fixture)); 370 | t.is(fixture.match(ipRegex.v4({includeBoundaries: true})), null); 371 | } 372 | 373 | for (const fixture of Object.keys(v4extract)) { 374 | t.deepEqual(fixture.match(ipRegex.v4()), v4extract[fixture]); 375 | } 376 | }); 377 | 378 | test('ip v6', t => { 379 | for (const fixture of v6) { 380 | t.true(ipRegex.v6({exact: true}).test(fixture)); 381 | } 382 | 383 | for (const fixture of v6) { 384 | t.is((ipRegex.v6().exec(`foo ${fixture} bar`) || [])[0], fixture); 385 | } 386 | 387 | for (const fixture of v6) { 388 | t.true(ipRegex.v6().test(`foo${fixture}bar`)); 389 | t.false(ipRegex.v6({includeBoundaries: true}).test(`foo${fixture}bar`)); 390 | } 391 | 392 | for (const fixture of v6not) { 393 | t.false(ipRegex.v6({exact: true}).test(fixture)); 394 | } 395 | 396 | for (const fixture of Object.keys(v6boundaries)) { 397 | t.true(ipRegex.v6().test(fixture)); 398 | t.deepEqual(fixture.match(ipRegex.v6()), v6boundaries[fixture]); 399 | t.false(ipRegex.v6({includeBoundaries: true}).test(fixture)); 400 | t.is(fixture.match(ipRegex.v6({includeBoundaries: true})), null); 401 | } 402 | 403 | for (const fixture of Object.keys(v6extract)) { 404 | t.deepEqual(fixture.match(ipRegex.v6()), v6extract[fixture]); 405 | } 406 | }); 407 | --------------------------------------------------------------------------------