Regular Expressions: Replace a particular submatch group with something
===

<!-- language-all: lang-js --> 

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. 

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(that_regexp,"<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).

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

I've already found one potential solution 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.