user1043994
user1043994

Reputation:

Regex to find URLs in a string

I'm using a regex to find URLs in a string, and then turn them into real HTML links, (in JavaScript). The problem with my regex is that it includes the previous character before http. (I'm talking about the second regex in the first array.)

str = "testhttp://example.com";

search = new Array(
    /\[url\](.*?)\[\/url\]/ig,
    /(?:[^\]\/">]|^)((https?):\/\/[-A-ZÅÄÖ0-9+&@#\/%?=~_|!:,.;]*[-A-ZÅÄÖ0-9+&@#\/%=~_|])/ig
);

replace = new Array(
    '<a href="//$1">$1</a>',
    '<a href="$1">$1</a>'
);

for (i = 0; i < search.length; i++) {
    str = str.replace(search[i], replace[i]);
}

The output becomes:

tes<a href="http://example.com">http://example.com</a>

But I want it to be:

test<a href="http://example.com">http://example.com</a>

What's important is that the regex should find URLs even though they are first in the string, but they should not be found if the previous character is either one of the following three characters: "/>

I'm quite new to regex. Hope you understand!

Thanks!

Upvotes: 1

Views: 333

Answers (2)

Martin Ender
Martin Ender

Reputation: 44289

The problem is that JavaScript will always replace the full match, not an inner capture group.

So here is a neat (and tested) trick to alleviate this. Make your first subpattern capturing:

/([^\]\/">]|^)((https?):\/\/[-A-ZÅÄÖ0-9+&@#\/%?=~_|!:,.;]*[-A-ZÅÄÖ0-9+&@#\/%=~_|])/ig

And then include it explicitly:

'$1<a href="$2">$2</a>'

Upvotes: 1

Eric
Eric

Reputation: 97671

Use a look behind:

 /(?<[^\]\/">]|^)((https?):\/\/[-A-ZÅÄÖ0-9+&@#\/%?=~_|!:,.;]*[-A-ZÅÄÖ0-9+&@#\/%=~_|])/ig 

Upvotes: 0

Related Questions