Replace text in string with hyperlinks using regular expression

Discussion in 'JavaScript' started by lbalance, Jan 9, 2010.

  1. #1
    I am trying to replace all URL's in an AJAX string into a hyperlink called [LINK]. Below I have code that is very close. It's breaking on long URL's with - (hyphens).

    Can we:
    1. Fix the regex to look for hyphens?
    or
    2. Write a regex to look for http and grab everything up until the first space?

    result=result.replace(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/g,'<a href="$1" target="_blank">[LINK]</a>');
    
    Code (markup):
     
    lbalance, Jan 9, 2010 IP