pax_global_header00006660000000000000000000000064134555066050014523gustar00rootroot0000000000000052 comment=ebdcc9c7b4136ba3c7503730e4bf92ba855f136f ip-regex-4.1.0/000077500000000000000000000000001345550660500132455ustar00rootroot00000000000000ip-regex-4.1.0/.editorconfig000066400000000000000000000002571345550660500157260ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ip-regex-4.1.0/.gitattributes000066400000000000000000000000231345550660500161330ustar00rootroot00000000000000* text=auto eol=lf ip-regex-4.1.0/.gitignore000066400000000000000000000000271345550660500152340ustar00rootroot00000000000000node_modules yarn.lock ip-regex-4.1.0/.npmrc000066400000000000000000000000231345550660500143600ustar00rootroot00000000000000package-lock=false ip-regex-4.1.0/.travis.yml000066400000000000000000000000541345550660500153550ustar00rootroot00000000000000language: node_js node_js: - '10' - '8' ip-regex-4.1.0/index.d.ts000066400000000000000000000030261345550660500151470ustar00rootroot00000000000000declare namespace ip { interface Options { /** 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)* @default false */ readonly exact?: boolean; /** 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). @default false */ readonly includeBoundaries?: boolean; } } declare const ip: { /** Regular expression for matching IP addresses. @returns A regex for matching both IPv4 and IPv6. @example ``` import ipRegex = require('ip-regex'); // Contains an IP address? ipRegex().test('unicorn 192.168.0.1'); //=> true // Is an IP address? ipRegex({exact: true}).test('unicorn 192.168.0.1'); //=> false 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex()); //=> ['192.168.0.1', '1:2:3:4:5:6:7:8'] // Contains an IP address? ipRegex({includeBoundaries: true}).test('192.168.0.2000000000'); //=> false // Matches an IP address? '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true})); //=> null ``` */ (options?: ip.Options): RegExp; /** @returns A regex for matching IPv4. */ v4(options?: ip.Options): RegExp; /** @returns A regex for matching IPv6. @example ``` import ipRegex = require('ip-regex'); ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8'); //=> true ``` */ v6(options?: ip.Options): RegExp; }; export = ip; ip-regex-4.1.0/index.js000066400000000000000000000036531345550660500147210ustar00rootroot00000000000000'use strict'; const word = '[a-fA-F\\d:]'; const b = options => options && options.includeBoundaries ? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))` : ''; 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}'; const v6seg = '[a-fA-F\\d]{1,4}'; const v6 = ` ( (?:${v6seg}:){7}(?:${v6seg}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 (?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)| // 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 (?:${v6seg}:){5}(?::${v4}|(:${v6seg}){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 (?:${v6seg}:){4}(?:(:${v6seg}){0,1}:${v4}|(:${v6seg}){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 (?:${v6seg}:){3}(?:(:${v6seg}){0,2}:${v4}|(:${v6seg}){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 (?:${v6seg}:){2}(?:(:${v6seg}){0,3}:${v4}|(:${v6seg}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4 (?:${v6seg}:){1}(?:(:${v6seg}){0,4}:${v4}|(:${v6seg}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4 (?::((?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4 )(%[0-9a-zA-Z]{1,})? // %eth0 %1 `.replace(/\s*\/\/.*$/gm, '').replace(/\n/g, '').trim(); const ip = options => options && options.exact ? new RegExp(`(?:^${v4}$)|(?:^${v6}$)`) : new RegExp(`(?:${b(options)}${v4}${b(options)})|(?:${b(options)}${v6}${b(options)})`, 'g'); ip.v4 = options => options && options.exact ? new RegExp(`^${v4}$`) : new RegExp(`${b(options)}${v4}${b(options)}`, 'g'); ip.v6 = options => options && options.exact ? new RegExp(`^${v6}$`) : new RegExp(`${b(options)}${v6}${b(options)}`, 'g'); module.exports = ip; ip-regex-4.1.0/index.test-d.ts000066400000000000000000000010001345550660500161120ustar00rootroot00000000000000import {expectType} from 'tsd'; import ipRegex = require('.'); const options: ipRegex.Options = {}; expectType(ipRegex()); expectType(ipRegex({exact: true})); expectType(ipRegex({includeBoundaries: true})); expectType(ipRegex.v4()); expectType(ipRegex.v4({exact: true})); expectType(ipRegex.v4({includeBoundaries: true})); expectType(ipRegex.v6()); expectType(ipRegex.v6({exact: true})); expectType(ipRegex.v6({includeBoundaries: true})); ip-regex-4.1.0/license000066400000000000000000000021251345550660500146120ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. ip-regex-4.1.0/package.json000066400000000000000000000012551345550660500155360ustar00rootroot00000000000000{ "name": "ip-regex", "version": "4.1.0", "description": "Regular expression for matching IP addresses (IPv4 & IPv6)", "license": "MIT", "repository": "sindresorhus/ip-regex", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "ip", "ipv6", "ipv4", "regex", "regexp", "re", "match", "test", "find", "text", "pattern", "internet", "protocol", "address", "validate" ], "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.2", "xo": "^0.24.0" } } ip-regex-4.1.0/readme.md000066400000000000000000000040731345550660500150300ustar00rootroot00000000000000# ip-regex [![Build Status](https://travis-ci.org/sindresorhus/ip-regex.svg?branch=master)](https://travis-ci.org/sindresorhus/ip-regex) > Regular expression for matching IP addresses ## Install ``` $ npm install ip-regex ``` This module targets Node.js 8 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` ## Usage ```js const ipRegex = require('ip-regex'); // Contains an IP address? ipRegex().test('unicorn 192.168.0.1'); //=> true // Is an IP address? ipRegex({exact: true}).test('unicorn 192.168.0.1'); //=> false ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8'); //=> true 'unicorn 192.168.0.1 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex()); //=> ['192.168.0.1', '1:2:3:4:5:6:7:8'] // Contains an IP address? ipRegex({includeBoundaries: true}).test('192.168.0.2000000000'); //=> false // Matches an IP address? '192.168.0.2000000000'.match(ipRegex({includeBoundaries: true})); //=> null ``` ## API ### ipRegex([options]) Returns a regex for matching both IPv4 and IPv6. ### ipRegex.v4([options]) Returns a regex for matching IPv4. ### ipRegex.v6([options]) Returns a regex for matching IPv6. #### options Type: `Object` ##### exact Type: `boolean`
Default: `false` *(Matches any IP address in a string)* Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. ##### includeBoundaries Type: `boolean`
Default: `false` 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). ## Related - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation - [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation ## License MIT © [Sindre Sorhus](https://sindresorhus.com) ip-regex-4.1.0/test.js000066400000000000000000000217151345550660500145700ustar00rootroot00000000000000import test from 'ava'; import ipRegex from '.'; const v4 = [ '0.0.0.0', '8.8.8.8', '127.0.0.1', '100.100.100.100', '192.168.0.1', '18.101.25.153', '123.23.34.2', '172.26.168.134', '212.58.241.131', '128.0.0.0', '23.71.254.72', '223.255.255.255', '192.0.2.235', '99.198.122.146', '46.51.197.88', '173.194.34.134' ]; const v4not = [ '.100.100.100.100', '100..100.100.100.', '100.100.100.100.', '999.999.999.999', '256.256.256.256', '256.100.100.100.100', '123.123.123', 'http://123.123.123', '1000.2.3.4', '999.2.3.4' ]; const v4boundaries = { '0000000192.168.0.200': ['192.168.0.200'], '192.168.0.2000000000': ['192.168.0.200'] }; const v4extract = { '255.255.255.255 0.0.0.0': ['255.255.255.255', '0.0.0.0'], '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'], '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'], '192.168.0.2000000000': ['192.168.0.200'] }; const v6 = [ '::', '1::', '::1', '1::8', '1::7:8', '1:2:3:4:5:6:7:8', '1:2:3:4:5:6::8', '1:2:3:4:5:6:7::', '1:2:3:4:5::7:8', '1:2:3:4:5::8', '1:2:3::8', '1::4:5:6:7:8', '1::6:7:8', '1::3:4:5:6:7:8', '1:2:3:4::6:7:8', '1:2::4:5:6:7:8', '::2:3:4:5:6:7:8', '1:2::8', '2001:0000:1234:0000:0000:C1C0:ABCD:0876', '3ffe:0b00:0000:0000:0001:0000:0000:000a', 'FF02:0000:0000:0000:0000:0000:0000:0001', '0000:0000:0000:0000:0000:0000:0000:0001', '0000:0000:0000:0000:0000:0000:0000:0000', '::ffff:192.168.1.26', '2::10', 'ff02::1', 'fe80::', '2002::', '2001:db8::', '2001:0db8:1234::', '::ffff:0:0', '::ffff:192.168.1.1', '1:2:3:4::8', '1::2:3:4:5:6:7', '1::2:3:4:5:6', '1::2:3:4:5', '1::2:3:4', '1::2:3', '::2:3:4:5:6:7', '::2:3:4:5:6', '::2:3:4:5', '::2:3:4', '::2:3', '::8', '1:2:3:4:5:6::', '1:2:3:4:5::', '1:2:3:4::', '1:2:3::', '1:2::', '1:2:3:4::7:8', '1:2:3::7:8', '1:2::7:8', '1:2:3:4:5:6:1.2.3.4', '1:2:3:4:5::1.2.3.4', '1:2:3:4::1.2.3.4', '1:2:3::1.2.3.4', '1:2::1.2.3.4', '1::1.2.3.4', '1:2:3:4::5:1.2.3.4', '1:2:3::5:1.2.3.4', '1:2::5:1.2.3.4', '1::5:1.2.3.4', '1::5:11.22.33.44', 'fe80::217:f2ff:254.7.237.98', 'fe80::217:f2ff:fe07:ed62', '2001:DB8:0:0:8:800:200C:417A', 'FF01:0:0:0:0:0:0:101', '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:0', '2001:DB8::8:800:200C:417A', 'FF01::101', '0:0:0:0:0:0:13.1.68.3', '0:0:0:0:0:FFFF:129.144.52.38', '::13.1.68.3', '::FFFF:129.144.52.38', 'fe80:0000:0000:0000:0204:61ff:fe9d:f156', 'fe80:0:0:0:204:61ff:fe9d:f156', 'fe80::204:61ff:fe9d:f156', 'fe80:0:0:0:204:61ff:254.157.241.86', 'fe80::204:61ff:254.157.241.86', 'fe80::1', '2001:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:db8:85a3:0:0:8a2e:370:7334', '2001:db8:85a3::8a2e:370:7334', '2001:0db8:0000:0000:0000:0000:1428:57ab', '2001:0db8:0000:0000:0000::1428:57ab', '2001:0db8:0:0:0:0:1428:57ab', '2001:0db8:0:0::1428:57ab', '2001:0db8::1428:57ab', '2001:db8::1428:57ab', '::ffff:12.34.56.78', '::ffff:0c22:384e', '2001:0db8:1234:0000:0000:0000:0000:0000', '2001:0db8:1234:ffff:ffff:ffff:ffff:ffff', '2001:db8:a::123', '::ffff:192.0.2.128', '::ffff:c000:280', 'a:b:c:d:e:f:f1:f2', 'a:b:c::d:e:f:f1', 'a:b:c::d:e:f', 'a:b:c::d:e', 'a:b:c::d', '::a', '::a:b:c', '::a:b:c:d:e:f:f1', 'a::', 'a:b:c::', 'a:b:c:d:e:f:f1::', 'a:bb:ccc:dddd:000e:00f:0f::', '0:a:0:a:0:0:0:a', '0:a:0:0:a:0:0:a', '2001:db8:1:1:1:1:0:0', '2001:db8:1:1:1:0:0:0', '2001:db8:1:1:0:0:0:0', '2001:db8:1:0:0:0:0:0', '2001:db8:0:0:0:0:0:0', '2001:0:0:0:0:0:0:0', 'A:BB:CCC:DDDD:000E:00F:0F::', '0:0:0:0:0:0:0:a', '0:0:0:0:a:0:0:0', '0:0:0:a:0:0:0:0', 'a:0:0:a:0:0:a:a', 'a:0:0:a:0:0:0:a', 'a:0:0:0:a:0:0:a', 'a:0:0:0:a:0:0:0', 'a:0:0:0:0:0:0:0', 'fe80::7:8%eth0', 'fe80::7:8%1' ]; const v6not = [ '', '1:', ':1', '11:36:12', '02001:0000:1234:0000:0000:C1C0:ABCD:0876', '2001:0000:1234:0000:00001:C1C0:ABCD:0876', '2001:0000:1234: 0000:0000:C1C0:ABCD:0876', '2001:1:1:1:1:1:255Z255X255Y255', '3ffe:0b00:0000:0001:0000:0000:000a', 'FF02:0000:0000:0000:0000:0000:0000:0000:0001', '3ffe:b00::1::a', '::1111:2222:3333:4444:5555:6666::', '1:2:3::4:5::7:8', '12345::6:7:8', '1::5:400.2.3.4', '1::5:260.2.3.4', '1::5:256.2.3.4', '1::5:1.256.3.4', '1::5:1.2.256.4', '1::5:1.2.3.256', '1::5:300.2.3.4', '1::5:1.300.3.4', '1::5:1.2.300.4', '1::5:1.2.3.300', '1::5:900.2.3.4', '1::5:1.900.3.4', '1::5:1.2.900.4', '1::5:1.2.3.900', '1::5:300.300.300.300', '1::5:3000.30.30.30', '1::400.2.3.4', '1::260.2.3.4', '1::256.2.3.4', '1::1.256.3.4', '1::1.2.256.4', '1::1.2.3.256', '1::300.2.3.4', '1::1.300.3.4', '1::1.2.300.4', '1::1.2.3.300', '1::900.2.3.4', '1::1.900.3.4', '1::1.2.900.4', '1::1.2.3.900', '1::300.300.300.300', '1::3000.30.30.30', '::400.2.3.4', '::260.2.3.4', '::256.2.3.4', '::1.256.3.4', '::1.2.256.4', '::1.2.3.256', '::300.2.3.4', '::1.300.3.4', '::1.2.300.4', '::1.2.3.300', '::900.2.3.4', '::1.900.3.4', '::1.2.900.4', '::1.2.3.900', '::300.300.300.300', '::3000.30.30.30', '2001:DB8:0:0:8:800:200C:417A:221', 'FF01::101::2', '1111:2222:3333:4444::5555:', '1111:2222:3333::5555:', '1111:2222::5555:', '1111::5555:', '::5555:', ':::', '1111:', ':', ':1111:2222:3333:4444::5555', ':1111:2222:3333::5555', ':1111:2222::5555', ':1111::5555', ':::5555', '1.2.3.4:1111:2222:3333:4444::5555', '1.2.3.4:1111:2222:3333::5555', '1.2.3.4:1111:2222::5555', '1.2.3.4:1111::5555', '1.2.3.4::5555', '1.2.3.4::', 'fe80:0000:0000:0000:0204:61ff:254.157.241.086', '123', 'ldkfj', '2001::FFD3::57ab', '2001:db8:85a3::8a2e:37023:7334', '2001:db8:85a3::8a2e:370k:7334', '1:2:3:4:5:6:7:8:9', '1::2::3', '1:::3:4:5', '1:2:3::4:5:6:7:8:9', '::ffff:2.3.4', '::ffff:257.1.2.3', '::ffff:12345678901234567890.1.26' ]; const v6boundaries = { '02001:0000:1234:0000:0000:C1C0:ABCD:0876': ['2001:0000:1234:0000:0000:C1C0:ABCD:0876'], 'fe80:0000:0000:0000:0204:61ff:fe9d:f156245': ['fe80:0000:0000:0000:0204:61ff:fe9d:f156'] }; const v6extract = { '::1, ::2, ::3-::5': ['::1', '::2', '::3', '::5'], '::1 ::2 ::3 - ::5': ['::1', '::2', '::3', '::5'], '::ffff:192.168.1.1 1::1.2.3.4': ['::ffff:192.168.1.1', '1::1.2.3.4'], '02001:0000:1234:0000:0000:C1C0:ABCD:0876 a::xyz': ['2001:0000:1234:0000:0000:C1C0:ABCD:0876', 'a::'] }; test('ip', t => { for (const x of v4) { t.true(ipRegex({exact: true}).test(x)); } for (const x of v4) { t.is((ipRegex().exec(`foo ${x} bar`) || [])[0], x); } for (const x of v4) { t.true(ipRegex().test(`foo${x}bar`)); t.false(ipRegex({includeBoundaries: true}).test(`foo${x}bar`)); } for (const x of v4not) { t.false(ipRegex({exact: true}).test(x)); } for (const x of v6) { t.true(ipRegex({exact: true}).test(x)); } for (const x of v6) { t.is((ipRegex().exec(`foo ${x} bar`) || [])[0], x); } for (const x of v6) { t.true(ipRegex().test(`foo${x}bar`)); t.false(ipRegex({includeBoundaries: true}).test(`foo${x}bar`)); } for (const x of v6not) { t.false(ipRegex({exact: true}).test(x)); } for (const x of Object.keys(v4boundaries)) { t.true(ipRegex().test(x)); t.deepEqual(x.match(ipRegex()), v4boundaries[x]); t.false(ipRegex({includeBoundaries: true}).test(x)); t.is(x.match(ipRegex({includeBoundaries: true})), null); } for (const x of Object.keys(v4extract)) { t.deepEqual(x.match(ipRegex()), v4extract[x]); } for (const x of Object.keys(v6boundaries)) { t.true(ipRegex().test(x)); t.deepEqual(x.match(ipRegex()), v6boundaries[x]); t.false(ipRegex({includeBoundaries: true}).test(x)); t.is(x.match(ipRegex({includeBoundaries: true})), null); } for (const x of Object.keys(v6extract)) { t.deepEqual(x.match(ipRegex()), v6extract[x]); } }); test('ip v4', t => { for (const x of v4) { t.true(ipRegex.v4({exact: true}).test(x)); } for (const x of v4) { t.is((ipRegex.v4().exec(`foo ${x} bar`) || [])[0], x); } for (const x of v4) { t.true(ipRegex.v4().test(`foo${x}bar`)); t.false(ipRegex.v4({includeBoundaries: true}).test(`foo${x}bar`)); } for (const x of v4not) { t.false(ipRegex.v4({exact: true}).test(x)); } for (const x of Object.keys(v4boundaries)) { t.true(ipRegex.v4().test(x)); t.deepEqual(x.match(ipRegex.v4()), v4boundaries[x]); t.false(ipRegex.v4({includeBoundaries: true}).test(x)); t.is(x.match(ipRegex.v4({includeBoundaries: true})), null); } for (const x of Object.keys(v4extract)) { t.deepEqual(x.match(ipRegex.v4()), v4extract[x]); } }); test('ip v6', t => { for (const x of v6) { t.true(ipRegex.v6({exact: true}).test(x)); } for (const x of v6) { t.is((ipRegex.v6().exec(`foo ${x} bar`) || [])[0], x); } for (const x of v6) { t.true(ipRegex.v6().test(`foo${x}bar`)); t.false(ipRegex.v6({includeBoundaries: true}).test(`foo${x}bar`)); } for (const x of v6not) { t.false(ipRegex.v6({exact: true}).test(x)); } for (const x of Object.keys(v6boundaries)) { t.true(ipRegex.v6().test(x)); t.deepEqual(x.match(ipRegex.v6()), v6boundaries[x]); t.false(ipRegex.v6({includeBoundaries: true}).test(x)); t.is(x.match(ipRegex.v6({includeBoundaries: true})), null); } for (const x of Object.keys(v6extract)) { t.deepEqual(x.match(ipRegex.v6()), v6extract[x]); } });