簡體   English   中英

使用正則表達式從字符串中提取 IP 地址

[英]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

以上是我從字符串中提取ip地址的代碼。 但它沒有這樣做。 請建議失敗的地方。

您已將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM