[英]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.以上是我从字符串中提取ip地址的代码。 But it fails to do so.
但它没有这样做。 Kindly advice where it fails.
请建议失败的地方。
You have defined r
as string, initialize it as regular expression.您已将
r
定义为字符串,将其初始化为正则表达式。
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.