ip-port-regex
v2.0.0
Published
Regular expression for matching IP:PORT addresses
Downloads
894
Maintainers
Readme
ip-port-regex
Regular expression for matching IP addresses with associated ports
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.
:copyright: ahmadnassri.com · License: ISC · Github: @ahmadnassri · Twitter: @ahmadnassri