Regex to Match IPv4 Addresses

Validate and match IPv4 addresses with this regex pattern. Ensures each octet is within the valid 0-255 range. Free accurate IP address regex.

Regular Expression

/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g

Token Breakdown

TokenDescription
\bWord boundary assertion
(?:Start of non-capturing group
(?:Start of non-capturing group
2Matches the literal character '2'
5Matches the literal character '5'
[0-5]Character class — matches any one of: 0-5
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-4]Character class — matches any one of: 0-4
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
[01]Character class — matches any one of: 01
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
\dMatches any digit (0-9)
?Makes the preceding element optional (zero or one times)
)End of group
\.Matches a literal dot
)End of group
{3}Matches exactly 3 times
(?:Start of non-capturing group
2Matches the literal character '2'
5Matches the literal character '5'
[0-5]Character class — matches any one of: 0-5
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-4]Character class — matches any one of: 0-4
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
[01]Character class — matches any one of: 01
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
\dMatches any digit (0-9)
?Makes the preceding element optional (zero or one times)
)End of group
\bWord boundary assertion

Detailed Explanation

This regex validates IPv4 addresses by ensuring each of the four octets falls within the range 0 to 255. Here is the token-by-token breakdown:

\b — A word boundary anchor that ensures the match starts at a boundary between a word character and a non-word character. This prevents partial matches within longer numbers.

(?: — Opens a non-capturing group for the first three octets (each followed by a dot).

(?:25[0-5]|2[0-4]\d|[01]?\d\d?) — A non-capturing group with three alternatives to match numbers 0-255:

  • 25[0-5] matches 250-255
  • 2[0-4]\d matches 200-249
  • [01]?\d\d? matches 0-199. The [01]? optionally matches a leading 0 or 1, \d matches one digit, and \d? optionally matches a second digit.

. — Matches a literal dot separating the octets.

){3} — Closes the non-capturing group and repeats it exactly three times, matching the first three octets with their trailing dots.

(?:25[0-5]|2[0-4]\d|[01]?\d\d?) — The same alternation pattern again for the fourth and final octet, but without a trailing dot.

\b — Another word boundary anchor at the end.

The g flag enables global matching to find all IPv4 addresses in the text. This pattern is highly accurate because it validates the numeric range of each octet rather than just matching digit patterns.

Example Test Strings

InputExpected
192.168.1.1Match
255.255.255.255Match
256.1.1.1No Match
10.0.0.1Match
192.168.1.999No Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

192.168.1.1 255.255.255.255 256.1.1.1 10.0.0.1 192.168.1.999

Matches & Capture Groups

#1192.168.1.1index 0
#2255.255.255.255index 12
#310.0.0.1index 38
Pattern: 75 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →