# ip-port-regex [![version][npm-version]][npm-url] [![License][npm-license]][license-url]

> Regular expression for matching IP addresses with associated ports

[![Build Status][travis-image]][travis-url]
[![Downloads][npm-downloads]][npm-url]
[![Code Climate][codeclimate-quality]][codeclimate-url]
[![Coverage Status][codeclimate-coverage]][codeclimate-url]
[![Dependency Status][dependencyci-image]][dependencyci-url]
[![Dependencies][david-image]][david-url]

## Install

```bash
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` .

```js
/*
 * 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')
```

```js
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](https://www.ahmadnassri.com/) &nbsp;&middot;&nbsp;
> License: [ISC][license-url] &nbsp;&middot;&nbsp;
> Github: [@ahmadnassri](https://github.com/ahmadnassri) &nbsp;&middot;&nbsp;
> Twitter: [@ahmadnassri](https://twitter.com/ahmadnassri)

[license-url]: http://choosealicense.com/licenses/isc/

[travis-url]: https://travis-ci.org/ahmadnassri/ip-port-regex
[travis-image]: https://img.shields.io/travis/ahmadnassri/ip-port-regex.svg?style=flat-square

[npm-url]: https://www.npmjs.com/package/ip-port-regex
[npm-license]: https://img.shields.io/npm/l/ip-port-regex.svg?style=flat-square
[npm-version]: https://img.shields.io/npm/v/ip-port-regex.svg?style=flat-square
[npm-downloads]: https://img.shields.io/npm/dm/ip-port-regex.svg?style=flat-square

[codeclimate-url]: https://codeclimate.com/github/ahmadnassri/ip-port-regex
[codeclimate-quality]: https://img.shields.io/codeclimate/github/ahmadnassri/ip-port-regex.svg?style=flat-square
[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/ahmadnassri/ip-port-regex.svg?style=flat-square

[david-url]: https://david-dm.org/ahmadnassri/ip-port-regex
[david-image]: https://img.shields.io/david/ahmadnassri/ip-port-regex.svg?style=flat-square

[dependencyci-url]: https://dependencyci.com/github/ahmadnassri/ip-port-regex
[dependencyci-image]: https://dependencyci.com/github/ahmadnassri/ip-port-regex/badge?style=flat-square
