检测FQDN和URL REGEX MATCH(Javascript)

时间:2012-02-02 16:10:42

标签: javascript regex match url-shortener

这与我发布的上一个问题无关。我需要一个正则表达式来检测FDQN,例如google.ca/和www.google.ca/(必须检测正斜杠)以及http://www.google.cahttps://www.stackoverflow.com等网址。有人可以帮我弄这个吗。我正在使用匹配(在javascript中)来检测这些FDQN和URL。对不起,如果这似乎是我之前的问题的重复,但它不是(更具体)。

我使用它来匹配Twitter的字符数。当他们检测到URL或FDQN时,他们会将URL(如果其https)压缩为21个字符,将其他URL压缩为20个字符(无论多长时间)。

2 个答案:

答案 0 :(得分:0)

是“google.ca/”FQDN吗?我想是的,即使这会解决http://uz/ 问题究竟是你究竟在寻找什么? :)

检查这个是否适合您:http://regexlib.com/redetails.aspx?regexp_id=1735&AspxAutoDetectCookieSupport=1

如果没有,regexplib.com是一个很好的来源,但我建议更准确/明确地定义您的要求。

答案 1 :(得分:0)

您可以检测到.且没有空格的任何内容,但可能会导致误报。

例如

var s = "This is not related to a previous question I posted. I need a regex to detect FDQN such as google.ca/ and www.google.ca/ (must detect the forward slash) as well as urls such as http://www.google.ca and https://www.stackoverflow.com. Can someone help me with this"

console.log(s.match(/(https?\:\/\/)?([a-z0-9\-]+\.)+[a-z0-9\-]{2,8}\/?/ig))

输出

[
"google.ca/",
"www.google.ca/", 
"http://www.google.ca", 
"https://www.stackoverflow.com"
]