使用正则表达式从字符串中提取IP地址 [英] Extract ip address from a string using regex
本文介绍了使用正则表达式从字符串中提取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
上面是我的代码,用于从字符串中提取IP地址. 但是它没有这样做. 请告知失败的地方.
The above is my code to extract ip address from a string. But it fails to do so. Kindly advice where it fails.
推荐答案
您已将r
定义为字符串,并将其初始化为正则表达式.
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);
alert(t[0])
这篇关于使用正则表达式从字符串中提取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文