Jump to content

Replace plain URLs with links


EKY32
 Share

Go to solution Solved by SmOke_N,

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]

Link to comment
Share on other sites

  • Moderators
  • Solution

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>")
EndFunc

Edit:

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>")
EndFunc
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...