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!
-
This question gets asked a lot. Do a search for "linkify URL".ridgerunner– ridgerunner2012年09月26日 01:10:00 +00:00Commented Sep 26, 2012 at 1:10
2 Answers 2
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>'
Use a look behind:
/(?<[^\]\/">]|^)((https?):\/\/[-A-ZÅÄÖ0-9+&@#\/%?=~_|!:,.;]*[-A-ZÅÄÖ0-9+&@#\/%=~_|])/ig
-
I didn't think JavaScript natively supports lookbehinds, does it?Martin Ender– Martin Ender2012年09月25日 17:33:24 +00:00Commented Sep 25, 2012 at 17:33
-
@m.buettner: Oh, not sure. I thought it was just negative lookbehinds.Eric– Eric2012年09月25日 17:35:22 +00:00Commented Sep 25, 2012 at 17:35
-
apparently not, I just ran the code. Also, this.Martin Ender– Martin Ender2012年09月25日 17:37:37 +00:00Commented Sep 25, 2012 at 17:37