Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:52:06+00:00 2026-06-18T00:52:06+00:00

I need the parse text with links in the following formats: [html title](http://www.htmlpage.com) http://www.htmlpage.com

  • 0

I need the parse text with links in the following formats:

[html title](http://www.htmlpage.com)
http://www.htmlpage.com
https://i.stack.imgur.com/rDDPu.jpg

The output for those two strings would be:

<a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>http://www.htmlpage.com</a>
<a href='https://i.stack.imgur.com/rDDPu.jpg'>https://i.stack.imgur.com/rDDPu.jpg</a>

The string could include an arbitrary amount of these links, ie:

[html title](http://www.htmlpage.com)[html title](http://www.htmlpage.com)
[html title](http://www.htmlpage.com)   [html title](http://www.htmlpage.com)
[html title](http://www.htmlpage.com) wejwelfj http://www.htmlpage.com

output:

<a href='http://www.htmlpage.com'>html title</a><a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>html title</a>    <a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>html title</a> wejwelfj <a href='http://www.htmlpage.com'>http://www.htmlpage.com</a>

I have an extremely long function that does an alright job by passing over the string 3 times, but I can’t successfully parse this string:

[This](http://i.imgur.com/iIlhrEu.jpg) one got me crying first, then once the floodgates were opened [this](http://i.imgur.com/IwSNFVD.jpg) one did it again and [this](http://i.imgur.com/hxIwPKJ.jpg). Ugh, feels. Gotta go hug someone/something.

For brevity, I’ll post the regular expressions I’ve tried rather than the entire find/replace function:

var matchArray2 = inString.match(/\[.*\]\(.*\)/g);

for matching [*](*), doesn’t work because []()[]() is matched

Really that’s it, I guess. Once I make that match I search that match for () and [] to parse out the link an link text and build the href tag. I delete matches from a temp string so I don’t match them when I do my second pass to find plain hyperlinks:

var plainLinkArray = tempString2.match(/http\S*:\/\/\S*/g);

I’m not parsing any html with regex. I’m parsing a string and attempting to output html.

edit: I added the requirement that it parse the third link https://i.stack.imgur.com/rDDPu.jpg after the fact.

my final solution (based on @Cerbrus’s answer):

function parseAndHandleHyperlinks(inString)
{
    var result = inString.replace(/\[(.+?)\]\((https?:\/\/.+?)\)/g, '<a href="$2">$1</a>');
    return result.replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</a>');     
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:52:08+00:00Added an answer on June 18, 2026 at 12:52 am

    Try this regex:

    /\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g
    
    var s = "[html title](http://www.htmlpage.com)[html title](http://www.htmlpage.com)\n\
    [html title](http://www.htmlpage.com)   [html title](http://www.htmlpage.com)\n\
    [html title](http://www.htmlpage.com) wejwelfj http://www.htmlpage.com";
    
    s.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>');
    

    Regex Explanation:

    # /                   - Regex Start
    # \[                  - a `[` character (escaped)
    # (.+?)               - Followed by any amount of words, grouped, non-greedy, so it won't match past:
    # \]                  - a `]` character (escaped)
    # \(                  - Followed by a `(` character (escaped)
    # (https?:\/\/
    #   [a-zA-Z0-9/.(]+?) - Followed by a string that starts with `http://` or `https://`
    # \)                  - Followed by a `)` character (escaped)
    # /g                  - End of the regex, search globally.
    

    Now the 2 strings in the () / [] are captured, and placed in the following string:

    '<a href="$2">$1</a>';
    

    This works for your "problematic" string:

    var s = "[This](http://i.imgur.com/iIlhrEu.jpg) one got me crying first, then once the floodgates were opened [this](http://i.imgur.com/IwSNFVD.jpg) one did it again and [this](http://i.imgur.com/hxIwPKJ.jpg). Ugh, feels. Gotta go hug someone/something."
    s.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>')
    
    // Result:
    
    '<a href="http://i.imgur.com/iIlhrEu.jpg">This</a> one got me crying first, then once the floodgates were opened <a href="http://i.imgur.com/IwSNFVD.jpg">this</a> one did it again and <a href="http://i.imgur.com/hxIwPKJ.jpg">this</a>. Ugh, feels. Gotta go hug someone/something.'
    

    Some more examples with "Incorrect" input:

    var s = "[Th][][is](http://x.com)\n\
        [this](http://x(.com)\n\
        [this](http://x).com)"
    s.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>')
    
    //   "<a href="http://x.com">Th][][is</a>
    //    <a href="http://x(.com">this</a>
    //    <a href="http://x">this</a>.com)"
    

    You can’t really blame the last line for breaking, since there’s no way to know if the user meant to stop the url there, or not.

    To catch loose urls, add this:

    .replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</a>');
    

    The (?: |^) bit catches a String start or space character, so it’ll also match lines starting with a url.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need the Perl regex to parse plain text input and convert all links
I am trying to parse the following HTML. I need to get the innertext
I need to parse some text in a UITextField and turn it into a
I need a regular expression to parse a text The directory is /home/foo/bar/hello.txt. I
I wanted to parse a text file that contains unstructured text. I need to
I need a regular expression to parse a body of text. Basically assume this
Possible Duplicate: How to parse and process HTML with PHP? I need to parse
I need to parse/read a lot of HTML webpages (100+) for specific content (a
I need an advice. I need to scrap and parse text file (using for
Given the following example: sites=c('site 1','site 2') link=c('<a href=http://example.com/path>This website</a>', '<a href=http://example.com/path2>That website</a>') w=data.frame(link,sites)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.