Tom
Tom

Reputation: 5994

How to add a TLD check to a RegExp?

I'm using the RegExp below to find links in a string. I need to extend it with a check for all domain TLDs (.com, travel, .name, etc).

var str = "check example.com or www.example.com or http://example.com or https://example.com or www.example.travel";

var filter:RegExp = /((https?:\/\/|www\.| |\*)[äöüa-z0-9\-\:\/]{1,}+\.[\*\!\'\(\)\;\:\@\&\=\$\,\?\#\%\[\]\~\-\+\_äöüa-z0-9\/\.]{2,}+)/gi

var matches = str.match(filter)

if (matches !== null) {
trace("Found: " + matches);
} else {
trace("nothing found");
}

I think it needs to be something like this [.com|.travel|.name] but how to implement it?

Upvotes: 1

Views: 66

Answers (1)

null
null

Reputation: 5255

I think it's a lot easier to do this in two steps.

  1. Find the links as you do it at the moment.
  2. from the matched strings, isolate the domain with a separate pattern (a dot followed by letters) and check if the results of this match (if there are any) are in the TLD list. (indexOf() for example, depending on the list you have, or use the result as a pattern to match the list))

Upvotes: 1

Related Questions