Sign in to follow this  
Followers 0
EKY32

Replace plain URLs with links

3 posts in this topic

#1 ·  Posted (edited)

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



#2 ·  Posted (edited)

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
1 person likes this

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

#3 ·  Posted (edited)

That's it thank you very much. :bye:

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

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  
Followers 0