Jump to content

Using regex to make a link clickable


Recommended Posts

Hello friends! I am rather new to using regular expressions. So please mind my noob question.

 

I have an instant messenger and I use in it an embeded IE control to display messages.

 

How would I go about detecting links (http and https) and turn them into anchors <a href="https://someurl.org/blahblah">https://someurl.org/blahblah

 

Thanks much in advance.

Link to comment
Share on other sites

You can make a RegEx (or find it from the internet) for detecting the URLs and append the detected urls with the HTML tags... If you actually do not know how RegEx works, I suggest you to wait for someone else with a magical RegEx pattern to post here, we have a lot of RegEx wizards in the forums ;)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

This raw example assumes that the links in the text are ending with white spaces. If it is not the case then the pattern must be adapted in the regular expression

Local $text = "here is a link https://someurl.org/blahblah in the text, " & @crlf & _ 
        "and here is http://otherurl.org/blah another one"
        
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

 

Edited by mikell
Link to comment
Share on other sites

16 hours ago, mikell said:

This raw example assumes that the links in the text are ending with white spaces. If it is not the case then the pattern must be adapted in the regular expression

Local $text = "here is a link https://someurl.org/blahblah in the text, " & @crlf & _ 
        "and here is http://otherurl.org/blah another one"
        
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

 

Thanks, this works perfect for links that are followed by a white-space, but what about raw links sent without any white spaces behind them? How to go about detecting a link when not followed by a space? (how do I detect the end of the link when there's nothing following it)

Link to comment
Share on other sites

Did you try my code in this case ?

Local $text = "here is a link https://someurl.org/blahblah"     
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

If it doesn't work then please provide all the precise requirements - may links be enclosed by other characters and which ones
The best would be to post a txt with some examples of the string(s) you have to treat

Edited by mikell
Link to comment
Share on other sites

On 7/16/2017 at 1:40 AM, mikell said:

Did you try my code in this case ?

Local $text = "here is a link https://someurl.org/blahblah"     
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

If it doesn't work then please provide all the precise requirements - may links be enclosed by other characters and which ones
The best would be to post a txt with some examples of the string(s) you have to treat

Okay this code is working as I need it to, no changes needed!

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...