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 = /* * Node 6 */const ip-port-regex = /* * Node 4 (Default) * Note: additional ES2015 polyfills may be required */var ip-port-regex =
var ipRegex = ; // contains a valid address;//=> true // is a valid address;//=> false ipRegex;//=> true 'unicorn 192.168.0.1:8080 cake 1:2:3:4:5:6:7:8 rainbow';//=> ['192.168.0.1', '1:2:3:4:5:6:7:8'] ipRegex;//=> { ip: '192.168.0.1', port: '8080' } ipRegex;//=> { 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