or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-ip-regex

Regular expression for matching IP addresses (IPv4 & IPv6)

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ip-regex@5.0.x

To install, run

npx @tessl/cli install tessl/npm-ip-regex@5.0.0

index.mddocs/

IP Regex

IP 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.

Package Information

  • Package Name: ip-regex
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install ip-regex

Core Imports

import ipRegex from 'ip-regex';

For CommonJS:

const ipRegex = require('ip-regex');

For ES modules in JavaScript:

import ipRegex from 'ip-regex';

Basic Usage

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');
//=> false

Capabilities

Main IP Regex Function

Returns 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;

IPv4 Regex Function

Returns a regular expression for matching IPv4 addresses only.

/**
 * @param options - Configuration options
 * @returns A regex for matching IPv4
 */
ipRegex.v4(options?: Options): RegExp;

IPv6 Regex Function

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']

Types

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;
}

Configuration Options

exact option

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');   //=> true

includeBoundaries option

When 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']

Common Use Cases

IP Address Validation

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');     //=> false

IPv6 Zone ID Support

The regex supports IPv6 zone identifiers (scope IDs):

ipRegex.v6({exact: true}).test('fe80::1%eth0');     //=> true
ipRegex.v6({exact: true}).test('::1%1');            //=> true

Text Processing and Extraction

function 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']

Safe IP Detection with Boundaries

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']