简体   繁体   English

检测FQDN和URL REGEX MATCH(JavaScript)

[英]Detect FQDN and URL REGEX MATCH (Javascript)

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 . 我需要一个正则表达式来检测FDQN,例如google.ca/和www.google.ca/(必须检测正斜杠)以及url(例如http://www.google.cahttps://www.stackoverflow) .com Can someone help me with this. 有人可以帮我弄这个吗。 I am using match (in javascript) to detect these FDQN and URL. 我正在使用match(在javascript中)检测这些FDQN和URL。 Sorry if this seems to be a repeat to my previous question but it isn't (more specific). 抱歉,这似乎是我上一个问题的重复,但不是(更具体)。

I am using this to match Twitter's character count. 我正在使用它来匹配Twitter的字符数。 When they detect a URL or FDQN, they will compress the URL (if its https) to 21 characters and others to 20 characters (no matter how long it is). 当他们检测到URL或FDQN时,会将URL(如果是https)压缩为21个字符,将其他字符压缩为20个字符(无论长度如何)。

Is "google.ca/" FQDN? 是“ google.ca/”完整域名吗? I guess it is, if even this resolves http://uz/ The question really is what exactly are you searching for? 我想是的,即使这也解决了http:// uz /问题,实际上是您到底在搜索什么? :) :)

Check if this one works for you: http://regexlib.com/redetails.aspx?regexp_id=1735&AspxAutoDetectCookieSupport=1 检查这是否适合您: http : //regexlib.com/redetails.aspx?regexp_id=1735&AspxAutoDetectCookieSupport=1

If not, regexplib.com is a good source, but I would suggest defining your requirements more precisely/explicitly. 如果不是这样,regexplib.com是一个很好的来源,但是我建议您更精确/明确地定义您的需求。

You could just detect anything with a . 您可以使用来检测到任何东西. and no space, but its likely to cause false positives. 并且没有空格,但是可能会导致误报。

Eg. 例如。

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))

Output 输出量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM