11
 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.

MARKAND Bhatt
  • 2,428
  • 10
  • 47
  • 80

1 Answers1

27

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])
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Satpal
  • 132,252
  • 13
  • 159
  • 168