简体   繁体   中英

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 . Can someone help me with this. I am using match (in javascript) to detect these FDQN and 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. 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).

Is "google.ca/" FQDN? I guess it is, if even this resolves http://uz/ The question really is what exactly are you searching for? :)

Check if this one works for you: 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.

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"
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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