简体   繁体   中英

Extract ip address from a string using regex

 var r = "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; //http://www.regular-expressions.info/examples.html

var a = "http://www.example.com/landing.aspx?referrer=10.11.12.13";

var t = a.match(r); //Expecting the ip address as a result but gets null

The above is my code to extract ip address from a string. But it fails to do so. Kindly advice where it fails.

You have defined r as string, initialize it as regular expression.

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

 var r = /\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b/; //http://www.regular-expressions.info/examples.html var a = "http://www.example.com/landing.aspx?referrer=10.11.12.13"; var t = a.match(r); console.log(t[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM