mffap
mffap

Reputation: 489

Javascript match regex links

Hi i have the following text in a div:

[http://www.google.com Google this link] some other random text [http://example.com and another website] but don't parse [this is not a link, leave me alone].

What I tried to do was to convert the links into normal html links. The Format is always like this, so it opens with a [ and then the url, followed by the link text and then a closing ]. But I only want to match links, not all text in square brackets.

I want to use the .match() function in javascript to do this task, but I wasn't able to figure out the regex expression (I only need the text parts that are links - the rest should be a simple split).

Any help would be apprechiated.

Upvotes: 2

Views: 1588

Answers (1)

MaxArt
MaxArt

Reputation: 22617

Why .match and not .replace?

string.replace(/\[(https?:\/\/[^\]\s]+)(?: ([^\]]*))?\]/g, "<a href='$1'>$2</a>");

Upvotes: 5

Related Questions