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!