0

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

Pointy
  • 405,095
  • 59
  • 585
  • 614
Nitrodbz
  • 1,276
  • 1
  • 15
  • 25
  • 2
    It's here on Stack already: http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address – nana Feb 02 '12 at 16:14
  • I have used a lot of regex but they only seem to detect http and https (they don't detect FDQN like www.google.ca/ and google.ca/ – Nitrodbz Feb 02 '12 at 16:23
  • @mistrfu thanks but didn't work (same issue above) – Nitrodbz Feb 02 '12 at 16:26

2 Answers2

0

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.

nana
  • 4,426
  • 1
  • 34
  • 48
0

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"
]
Sam Greenhalgh
  • 5,952
  • 21
  • 37