1

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!

asked Sep 25, 2012 at 17:15
1
  • This question gets asked a lot. Do a search for "linkify URL". Commented Sep 26, 2012 at 1:10

2 Answers 2

1

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>'
answered Sep 25, 2012 at 17:28
0
0

Use a look behind:

 /(?<[^\]\/">]|^)((https?):\/\/[-A-ZÅÄÖ0-9+&@#\/%?=~_|!:,.;]*[-A-ZÅÄÖ0-9+&@#\/%=~_|])/ig 
answered Sep 25, 2012 at 17:31
3
  • I didn't think JavaScript natively supports lookbehinds, does it? Commented Sep 25, 2012 at 17:33
  • @m.buettner: Oh, not sure. I thought it was just negative lookbehinds. Commented Sep 25, 2012 at 17:35
  • apparently not, I just ran the code. Also, this. Commented Sep 25, 2012 at 17:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.