ip-port-regex

2.0.0 • Public • Published

ip-port-regex version License

Regular expression for matching IP addresses with associated ports

Build Status Downloads Code Climate Coverage Status Dependency Status Dependencies

Install

npm install --production --save ip-port-regex

Usage

I recommend using an optimized build matching your Node.js environment version, otherwise, the standard require would work just fine with any version of Node >= v4.0 .

/*
 * Node 7
 */
const ip-port-regex = require('ip-port-regex/lib/node7')
 
/*
 * Node 6
 */
const ip-port-regex = require('ip-port-regex/lib/node6')
 
/*
 * Node 4 (Default)
 * Note: additional ES2015 polyfills may be required
 */
var ip-port-regex = require('ip-port-regex')
var ipRegex = require('ip-port-regex');
 
// contains a valid address
ipRegex().test('unicorn 192.168.0.1:8000');
//=> true
 
// is a valid address
ipRegex({exact: true}).test('unicorn 192.168.0.1:8001');
//=> false
 
ipRegex.v6({exact: true}).test('[1:2:3:4:5:6:7:8]:9000');
//=> true
 
'unicorn 192.168.0.1:8080 cake 1:2:3:4:5:6:7:8 rainbow'.match(ipRegex());
//=> ['192.168.0.1', '1:2:3:4:5:6:7:8']
 
ipRegex.parts('192.168.0.1:8080');
//=> { ip: '192.168.0.1', port: '8080' }
 
ipRegex.parts('[1:2:3:4:5:6:7:8]:8080');
//=> { ip: '192.168.0.1', port: '8080' }

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.

ipRegex.parts(string)

Returns an Object, containing found IP address + Port in string

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


©️ ahmadnassri.com  ·  License: ISC  ·  Github: @ahmadnassri  ·  Twitter: @ahmadnassri

Package Sidebar

Install

npm i ip-port-regex

Weekly Downloads

1,753

Version

2.0.0

License

ISC

Last publish

Collaborators

  • ahmadnassri