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 8231069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:21:08+00:00 2026-06-07T17:21:08+00:00

Here’s my situation. I want to recognize Markdown for a link (in this case

  • 0

Here’s my situation. I want to recognize Markdown for a link (in this case just one particular style of link is fine, it’s this format: [link text](url "optional title"), and what I’m trying to do is put this Markdown text into a <pre> tag with the url appropriately wrapped in an <a> tag.

A pseudoexample:

Convert

[link text](url "optional title")

to

[link text](<a href='url'>url</a> "optional title")

So I’ve dug up the very regex used by the Markdown parser which is this:

/*
text = text.replace(/
    (                           // wrap whole match in $1
        \[
        (
            (?:
                \[[^\]]*\]      // allow brackets nested one level
                |
                [^\[\]]         // or anything else
            )*
        )
        \]
        \(                      // literal paren
        [ \t]*
        ()                      // no id, so leave $3 empty
        <?(                     // href = $4
            (?:
                \([^)]*\)       // allow one level of (correctly nested) parens (think MSDN)
                |
                [^()\s]
            )*?
        )>?                
        [ \t]*
        (                       // $5
            (['"])              // quote char = $6
            (.*?)               // Title = $7
            \6                  // matching quote
            [ \t]*              // ignore any spaces/tabs between closing quote and )
        )?                      // title is optional
        \)
    )
/g, writeAnchorTag);
*/

text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?((?:\([^)]*\)|[^()\s])*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeAnchorTag);

The breakdown in the nice comment helps a lot to see what’s going on and clearly all I need to do is replace $4 submatch with <a href='$4'>$4</a>.

But of course I can’t just do str.replace(re,"<a href='$4'>$4</a>"); because that would replace my entire Markdown link markup (including the link text and optional title) with a plain link. I want the plain link to show up in the original Markdown so that it still looks just like the original Markdown in the <pre> (but now with a clickable link in it).

So, let’s see…

Extract $4:

var group_4 = str.replace(re, "$4"); // Does anybody know a more efficient way to do this? I'm not trying to replace I just need to get the 4th group

Well here I’m stuck because I want to stick "<a href='"+group_4+"'>"+group_4+"</a>" in as a replacement for $4.

Anybody have tips for me? I’m pretty sure this can be done, and I suspect it can be done elegantly as well.

I’ve already found one potential solution (which is wrong) which is to strip out the sections of the regex which are outside of group $4. I don’t think this will be sufficient because it does not do any actual link-detection based on the link content (i.e. you could define a Markdown-link using something that is not a real link at all). So I should use the original regex so as to be sure that what I am converting into an <a> is actually part of a (Markdown inline-style) link.

  • 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-07T17:21:10+00:00Added an answer on June 7, 2026 at 5:21 pm

    I think I have a way to attack the problem using what I already know. Simply replace with the original parts. This means there must be other sub-matches that cover the entirety of the expression before and after $4. Supposing there is a group $x that contains the match from the beginning up to $4 and another group $y that contains the match from the end of $4 to the end of the string, all I have to do is str.replace(re,"$x<a href='$4'>$4</a>$y") and be done with it.

    Now to see if it is possible to modify our regex to not change its accepted language while providing me these groups.

    Update: Looking at it for a bit longer it’s actually quite basic:

    str.replace(re,"[$2]($4 $5)")
    

    gets me 99% of the way there to fully replicating the original input, and the only part where this is incorrect is in the space between $4 and $5 which in the input is [ \t]* so all I have to do is wrap that into a new group in the original regex. I believe it will become $5 so it will be:

    /(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?((?:\([^)]*\)|[^()\s])*?)>?([ \t]*)((['"])(.*?)\6[ \t]*)?\))/g
                                                                          ^      ^
    

    Carats on the line below indicate where parens were added.

    str.replace(re,"[$2]($4$5$6)")
    

    should yield the exact original, so

    str.replace(re,"[$2](<a href='$4'>$4</a>$5$6)")
    

    ought to do it.

    Now what’s left is devising a way to only escape the HTML outside of these link constructs because I don’t want to escape the anchor tag. Hmmm.

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

Sidebar

Related Questions

Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
here i want to understand the architecture of bluez (Bluetooth Stack Protocol). I understood
Here is what I am trying to achieve in PHP: I have this string:

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.