# ip-regex

> Regular expression for matching IP addresses (IPv4 & IPv6)

Latest version **5.0.0** (published 2022-01-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install ip-regex
pnpm add ip-regex
yarn add ip-regex
bun add ip-regex
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2022-01-01 |
| First published | 2014-09-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 197 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | ip, ipv6, ipv4, regex, regexp, re, match, test, find, text, pattern, internet, protocol, address, validate |

## Links

- npm: https://www.npmjs.com/package/ip-regex
- Repository: https://github.com/sindresorhus/ip-regex
- Homepage: https://github.com/sindresorhus/ip-regex#readme
- Issues: https://github.com/sindresorhus/ip-regex/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/ip-regex

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2022-01-01
- 4.3.0 — 2021-01-22
- 4.2.0 — 2020-09-28
- 4.1.0 — 2019-04-17
- 4.0.0 — 2019-01-18
- 3.0.0 — 2018-04-03
- 2.1.0 — 2017-03-11
- 2.0.0 — 2017-01-09
- 1.0.3 — 2015-02-09
- 1.0.2 — 2014-12-10
- 1.0.1 — 2014-11-29
- 1.0.0 — 2014-09-06

## README

# ip-regex

> Regular expression for matching IP addresses

## Install

```sh
$ npm install ip-regex
```

This module targets Node.js 12 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, use version 2.1.0: `npm install ip-regex@2.1.0`

## Usage

```js
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

ipRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8');
//=> true

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

// Contains an IP address?
ipRegex({includeBoundaries: true}).test('192.168.0.2000000000');
//=> false

// Matches an IP address?
'192.168.0.2000000000'.match(ipRegex({includeBoundaries: true}));
//=> null
```

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

#### options

Type: `object`

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

##### includeBoundaries

Type: `boolean`\
Default: `false`

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

## Related

- [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
- [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
- [cidr-regex](https://github.com/silverwind/cidr-regex) - Regular expression for matching IP addresses in CIDR notation

---
_Source: https://npm.io/package/ip-regex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
