Skip to main content
Inspiring
June 12, 2009
Answered

regex question - converting text to url

  • June 12, 2009
  • 1 reply
  • 687 views

I'm using the following regular expression to convert text strings to <a href> links when displaying a text file on the web.

document.body.innerHTML=document.body.innerHTML.replace(/(http:\/\/)(.*)\s/gi, '<a href=$1$2>$1$2</a><br>'); in the onLoad attribute of the body tag.

I also want to use the same regular expression with the rereplace function on the the text file content so that the text links display as live links in the rss feed I'm creating. I tried to do the following which did not work and I'm not very good with regular expressions.I wondering if someone could point out where I'm going wrong.

<cfset bbcontent = '#REReplace("#bbcontent#", "/(http:\/\/)(.*)\s/gi", "<a href=$1$2>$1$2</a><br>", "ALL")#'>

    This topic has been closed for replies.
    Correct answer mack_
    \1\2
    ", "ALL")> ]]>

    Notes:

    - too many quotes and #'s (inside cfset in 99% of the cases you don't

    need to use #);

    - (*) - take everything until you hit the first space;

    - in ColdFusion backreferences are with \1,\2,...;

    - /g is given by the fourth parameter ("ALL");

    - /i there are different functions for case sensitive and case

    insensitive matches.

    Mack

    1 reply

    mack_Correct answer
    Participating Frequently
    June 12, 2009
    \1\2
    ", "ALL")> ]]>

    Notes:

    - too many quotes and #'s (inside cfset in 99% of the cases you don't

    need to use #);

    - (*) - take everything until you hit the first space;

    - in ColdFusion backreferences are with \1,\2,...;

    - /g is given by the fourth parameter ("ALL");

    - /i there are different functions for case sensitive and case

    insensitive matches.

    Mack

    Inspiring
    June 12, 2009

    Thanks