Jay
Jay

Reputation: 511

Regular expression - Find the first link in a text

I'm using the regular expression below to find the first link in a text. Currently this version numbers get misinterpreted as links:

ver.1.0
xx.11
v1.2

RegExp:

var findLink:RegExp = /([äöüÄÖÜa-zA-Z0-9\-\:\/]{2,}+\.[\*\!\'\(\)\;\:\@\&\=\$\,\?\#\%\[\]\~\-\+\_äöüÄÖÜa-zA-Z0-9\/\.]{2,}+)/g
var getLink = themessage.text.match(findLink)
trace(getLink[0])

Example text:

Lorem Ipsum ver.1.0 is simply dummy text xx.11 of the printing v1.2 and typesetting industry. example.com (example.com should only be found as a link)

How can I add the condition to my RegExp that the characters after the second dot (if there is one at all) needs to be a-z and have a minimum of 2 characters? So that example.co or www.example.co works but example.1 or 1.example.a is not interpreted as a URL.

Upvotes: 1

Views: 635

Answers (2)

Weaselgrease
Weaselgrease

Reputation: 13

For just identifying a URL in a string, this seems to work well for me.

/([fh]t{1,2}ps?:\/\/)?[\w-]+\.\w{2,4}/

It identifies HTTPS, HTTP, and FTP URLs, as well as URLs that don't have any of those prefixed.

var string:String;
var istr:int;
istr = string.search(/([fh]t{1,2}ps?:\/\/)?[\w-]+\.\w{2,4}/);

trace(string.slice(istr).indexOf(" "));

Upvotes: 0

Navin Nair Kottayil
Navin Nair Kottayil

Reputation: 563

You can use a regular expression for that:

var find:RegExp = / (https?\:\/\/[^ ]*)/g;
trace(myText.match(find));

The regular expression will look for

a whitespace
http
s but not necessarly
://
a bunch of characters that are not whitespaces

The ( ) are there to delimit which group of characters the RegExp should catch. The g parameters at the end tells the RegExp not to stop looking after the first match.

ADDENDUM

If you want to match an URL without http, this should work to get a word with at least one point inside:

/ ([a-z\:\/]+\.[a-z\.]+) /g

At least one character of lowercase alphabet, : and / A dot At least one character of lowercase alphabet and .

Upvotes: 1

Related Questions