Krishna Raj
Krishna Raj

Reputation: 866

how to check a string dot preceding with another?

here is my regexp which checks for links in input text.

message = message
        .replace(/(https?:\/\/)(www\.)*[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g,'<a href="$&" class="my_link" target="_blank">$&</a>')
        .replace(/(https?:\/\/){0}www\.[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g,'<a href="http://$&" class="my_link" target="_blank">$&</a>');

which works with http://google.com, https://google.com, http://google.co.in ,https://google.co.in, www.google.com and www.google.co.in. but not works for http://www.google.com. what should i do for that? if i input this URL, it outputs www.google.com" class="my_link" target="_blank">http://www.google.com what should i do now? thanks

update: for this link http://www.youtube.com/watch?v=SukTBSJJ4KM&feature=g-vrec&context=G2d692eeRVAAAAAAAACA it breaks at several points. what is the regexp that should be added to read address after http://www.youtube.com

Upvotes: 1

Views: 184

Answers (4)

Krishna Raj
Krishna Raj

Reputation: 866

Me done it.. little changes in the answer of M42 made it.

message = message
        .replace(/(https?:\/\/|)((w+\.)?[a-zA-Z0-9-]{1,}(\.[a-z]{2,}){1,}(\/\S*)?)/g,'<a href="http://$2" class="my_link" target="_blank">$1$2</a>')

which works with all links. thanks M42. :D

Upvotes: 1

Toto
Toto

Reputation: 91518

how about:

message = message
    .replace(/(https?:\/\/|)((www\.)?[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*)/g,'<a href="http://$2" class="my_link" target="_blank">$1$2</a>')

Edit according to question changes:

message = message
    .replace(/(https?:\/\/|)(\S+)/g,'<a href="http://$2" class="my_link" target="_blank">$1$2</a>')

Upvotes: 1

jon
jon

Reputation: 6246

I think I have it, see the fiddle here: http://jsfiddle.net/MWBFS/

Guts are:

/\b(www\.|http(s)*:\/\/)(\S+)/

Output from Fiddle is:

beginning <a href="http://google.co.in" class="my_link" target="_blank">google.co.in</a> end
beginning <a href="https://google.co.in" class="my_link" target="_blank">google.co.in</a> end
beginning <a href="http://google.com" class="my_link" target="_blank">google.com</a> end
beginning <a href="http://google.co.in" class="my_link" target="_blank">google.co.in</a> end
beginning <a href="http://google.com/" class="my_link" target="_blank">google.com/</a> end
beginning <a href="http://google.co.in/" class="my_link" target="_blank">google.co.in/</a> end
beginning <a href="http://www.google.com/" class="my_link" target="_blank">www.google.com/</a> end
beginning <a href="http://www.google.com/" class="my_link" target="_blank">www.google.com/</a> end

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

Try following pattern:


/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

OR try this pattern:


var re = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;

Upvotes: 0

Related Questions