Jump to content
Sign in to follow this  

Replace plain URLs with links

Recommended Posts

Hello, i'm not good using StringRegExp function, may I have some help to make a function that replaces the plain text links with regular HTML anchor tags?


I found a JavsScript sample that may help.

function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1'>$1</a>"); 

Thank you.

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Share this post

Link to post
Share on other sites

Did you try just copy and pasting?

Func _replaceURLWithHTMLLinks($sText)
    Local Const $sExp = "(?i)(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])"
    Return StringRegExpReplace($sText, $sExp, "<a href='$1'>$1</a>")


Although, I'd probably have changed it up a bit, something like:

ConsoleWrite(_replaceURLWithHTMLLinks("http://www.autoitscript.com/forum/topic/167033-replace-plain-urls-with-links/") & @CRLF)

ConsoleWrite(_replaceURLWithHTMLLinks("http://www.autoitscript.com/forum/topic/167033-replace-plain-urls-with-links/", "Click Me") & @CRLF)

Func _replaceURLWithHTMLLinks($sURL, $sOutText = "")
    $sOutText = ((IsKeyword($sOutText) Or Not StringLen($sOutText)) ? "$1" : _
        StringRegExpReplace($sOutText, "(\$|\\)", "\\$1"))
    Local Const $sExp = "(?i)(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])"
    Return StringRegExpReplace($sURL, $sExp, "<a href='$1'>" & $sOutText & "</a>")
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...