Regex for IPv4 address

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

Options
Matches

Examples

Note

Each octet is limited to 0–255 by the alternation, which a simple \d{1,3} would not do. It still accepts leading zeros like 01.

Matches in the example

  • "192.168.1.1"
  • "10.0.0.255"
  • "8.8.8.8"

Token by token

  1. \b a word boundary
  2. (?: a group, not capturing
  3. (?: a group, not capturing
  4. 25 the text “25”
  5. [0-5] one of: “0” to “5”
  6. | or
  7. 2 the character “2”
  8. [0-4] one of: “0” to “4”
  9. \d a digit (0–9)
  10. | or
  11. 1 the character “1”
  12. \d a digit (0–9)
  13. \d a digit (0–9)
  14. | or
  15. [1-9]? one of: “1” to “9”, optionally
  16. \d a digit (0–9)
  17. ) end of group
  18. \. the character “.”
  19. ){3} end of group, whole group exactly 3 times
  20. (?: a group, not capturing
  21. 25 the text “25”
  22. [0-5] one of: “0” to “5”
  23. | or
  24. 2 the character “2”
  25. [0-4] one of: “0” to “4”
  26. \d a digit (0–9)
  27. | or
  28. 1 the character “1”
  29. \d a digit (0–9)
  30. \d a digit (0–9)
  31. | or
  32. [1-9]? one of: “1” to “9”, optionally
  33. \d a digit (0–9)
  34. ) end of group
  35. \b a word boundary

Other patterns