Regular expression for matching IP addresses (IPv4 & IPv6)
npx @tessl/cli install tessl/npm-ip-regex@5.0.0IP Regex provides comprehensive regular expression functionality for matching and validating IP addresses in both IPv4 and IPv6 formats. It offers flexible configuration options including exact matching and boundary detection for precise validation and text processing.
npm install ip-regeximport ipRegex from 'ip-regex';For CommonJS:
const ipRegex = require('ip-regex');For ES modules in JavaScript:
import ipRegex from 'ip-regex';import ipRegex from '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
// Extract all IP addresses from text
'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']
// IPv6 validation
ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
//=> true
// Boundary detection prevents false positives
ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
//=> falseReturns a regular expression for matching both IPv4 and IPv6 addresses.
/**
* Regular expression for matching IP addresses
* @param options - Configuration options
* @returns A regex for matching both IPv4 and IPv6
*/
declare const ipRegex: {
(options?: Options): RegExp;
v4(options?: Options): RegExp;
v6(options?: Options): RegExp;
};
export default ipRegex;Returns a regular expression for matching IPv4 addresses only.
/**
* @param options - Configuration options
* @returns A regex for matching IPv4
*/
ipRegex.v4(options?: Options): RegExp;Returns a regular expression for matching IPv6 addresses only.
/**
* @param options - Configuration options
* @returns A regex for matching IPv6
*/
ipRegex.v6(options?: Options): RegExp;Usage Examples:
import ipRegex from 'ip-regex';
// IPv4 only matching
const ipv4Text = 'Server at 192.168.1.1 and backup at 10.0.0.1';
const ipv4Addresses = ipv4Text.match(ipRegex.v4());
//=> ['192.168.1.1', '10.0.0.1']
// IPv6 only matching
const ipv6Address = '2001:0DB8:85A3:0000:0000:8A2E:0370:7334';
ipRegex.v6({exact: true}).test(ipv6Address);
//=> true
// Mixed IPv4 and IPv6 extraction
const mixedText = 'IPv4: 192.168.1.1, IPv6: ::1, another IPv4: 127.0.0.1';
const allIPs = mixedText.match(ipRegex());
//=> ['192.168.1.1', '::1', '127.0.0.1']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;
}When set to true, the regex will only match complete strings that are valid IP addresses. This is useful for validation:
// Validation mode - entire string must be an IP
ipRegex({exact: true}).test('192.168.1.1'); //=> true
ipRegex({exact: true}).test('prefix 192.168.1.1'); //=> false
// Search mode (default) - finds IPs within text
ipRegex().test('prefix 192.168.1.1 suffix'); //=> trueWhen set to true, includes word boundaries to prevent false positives with trailing digits:
// Without boundaries (default) - may match partial numbers
'192.168.0.2000000000'.match(ipRegex());
//=> ['192.168.0.200'] (matches first valid part)
// With boundaries - prevents false positives
'192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
//=> null (no match due to trailing digits)
// Proper IP addresses still match correctly
'Valid IP: 192.168.0.200 here'.match(ipRegex({includeBoundaries: true}));
//=> ['192.168.0.200']function isValidIP(ip) {
return ipRegex({exact: true}).test(ip);
}
isValidIP('192.168.1.1'); //=> true
isValidIP('not an ip'); //=> false
isValidIP('192.168.1.1.extra'); //=> falseThe regex supports IPv6 zone identifiers (scope IDs):
ipRegex.v6({exact: true}).test('fe80::1%eth0'); //=> true
ipRegex.v6({exact: true}).test('::1%1'); //=> truefunction extractIPs(text) {
return text.match(ipRegex()) || [];
}
const logEntry = 'Connection from 192.168.1.100 to 2001:db8::1 failed';
const foundIPs = extractIPs(logEntry);
//=> ['192.168.1.100', '2001:db8::1']function findRealIPs(text) {
return text.match(ipRegex({includeBoundaries: true})) || [];
}
// Prevents false matches in serial numbers, IDs, etc.
const serialNumber = 'Device-192168001001-Rev2';
findRealIPs(serialNumber); //=> [] (no false positive)
const realLog = 'Connected to 192.168.1.1 successfully';
findRealIPs(realLog); //=> ['192.168.1.1']