Skip to content Skip to sidebar Skip to footer

Unable To Match Ip Regex In Javascript

Following is my code I am trying to validate IP addresses in front end, but somehow this Regex is not working - var patt = /[0-255]\.[0-255]\.[0-255]\.[0-255]/; var str = '90.89.99

Solution 1:

Regex ranges don't do what you think they do. A [x-y] range contains all characters in ascii from x to y.

Therefore [0-255] matches characters from 0 to 2 (aka 0, 1 and 2) and 5 or in short - [0125].


To match a number from 0 to 255, you could do:

\d\d?|1\d\d|2([0-4]\d|5[0-5])

The idea:

  • \d\d? - one or two digit numbers
  • 1\d\d - numbers from 100 to 199
  • 2[0-4]\d - numbers from 200 to 249.
  • 25[0-5] - numbers from 250 to 255

To match an entire IP, you could do:

^((\d\d?|1\d\d|2([0-4]\d|5[0-5]))\.){3}(\d\d?|1\d\d|2([0-4]\d|5[0-5]))$

Solution 2:

[...] denotes a "character class" meaning that it lists characters that should match there; it matches any single character from what you list. So your regex is checking each segment for a single character which is any of 0, 1, 2, or 5 (0-2 gives us 0, 1, and 2; then 5 gives us 5, and the second 5 is ignored). It should be checking for digits (\d), and 1-3 of them ({1,3}):

var patt = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;

You might also want ^ (start of input) and $ (end of input), so that foo123.123.123.123bar doesn't match:

var patt = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/$;

This is, of course, not sufficient to check that the IP is valid. It just checks that there are numbers where expected; it would happily call 891.262.999.42 valid.

If you want to properly validate it (e.g., that the numbers are in the valid ranges, filtering out invalid combinations of segments, etc), you could extract the IP part of this comprehensive URL validation regex (since URLs can contain IP addresses, it covers that). Thankfully, Diego and the other authors have nicely commented it, clearly identifying which bits do what:

//// Regular Expression for URL validation//// Author: Diego Perini// Updated: 2010/12/05// License: MIT//// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)//// Permission is hereby granted, free of charge, to any person// obtaining a copy of this software and associated documentation// files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use,// copy, modify, merge, publish, distribute, sublicense, and/or sell// copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following// conditions://// The above copyright notice and this permission notice shall be// included in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR// OTHER DEALINGS IN THE SOFTWARE.//// the regular expression composed & commented// could be easily tweaked for RFC compliance,// it was expressly modified to fit & satisfy// these test for an URL shortener:////   http://mathiasbynens.be/demo/url-regex//// Notes on possible differences from a standard/generic validation://// - utf-8 char class take in consideration the full Unicode range// - TLDs have been made mandatory so single names like "localhost" fails// - protocols have been restricted to ftp, http and https only as requested//// Changes://// - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255//   first and last IP address of each class is considered invalid//   (since they are broadcast/network addresses)//// - Added exclusion of private, reserved and/or local networks ranges//// - Made starting path slash optional (http://example.com?foo=bar)//// - Allow a dot (.) at the end of hostnames (http://example.com.)//// Compressed one-line versions://// Javascript version//// /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i//// PHP version//// _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$_iuS//var re_weburl = new RegExp(
  "^"+// protocol identifier"(?:(?:https?|ftp)://)"+// user:pass authentication"(?:\\S+(?::\\S*)?@)?"+"(?:"+// IP address exclusion// private & local networks"(?!(?:10|127)(?:\\.\\d{1,3}){3})"+"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})"+"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})"+// IP address dotted notation octets// excludes loopback network 0.0.0.0// excludes reserved space >= 224.0.0.0// excludes network & broacast addresses// (first & last IP address of each class)"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"+"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"+"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"+"|"+// host name"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)"+// domain name"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"+// TLD identifier"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))"+// TLD may end with dot"\\.?"+")"+// port number"(?::\\d{2,5})?"+// resource path"(?:[/?#]\\S*)?"+"$", "i"
);

Solution 3:

Here is a regex variation (based on ndn's answer above) that will check if the entire input text is a valid IP:

^(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))$

See regex demo

Here is a solution that can be used to extract valid IPs from larger text:

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

See another demo

Explanation:

  • ^ - start of string (replace with \b word boundary if you need not match at the beginning of string only)
  • (?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3} - 3 occurrences of
    • (?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5])) - 1- or 2-digit sequence, or a 1xx number (1\d{2}) or a range of integer numbers in-between 200-255 range (thanks to 2(?:[0-4]\d|5[0-5]))
    • \. - a literal period
  • (?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5])) - see above (just the last part of the IP address)
  • $ - end of string (replace with \b if you just need a whole word match).

NOTE: In JS, you can use the literal notation to declare these regexps, e.g.:

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

Post a Comment for "Unable To Match Ip Regex In Javascript"