Is there a better way of inserting my element into text using Jquery?
<p>I've written some jquery to solve this problem. 
Although it works, I can't help think my jquery is terrible &amp; was hoping somebody can tell me that I'm missing something and correct my code to use something more awesome. In particular the string concat that I'm doing (commented as looking 'vomit') is lame. Can anyone unlame it?</p>

<p>The problem is to take the HTML below and replace the text in braces {glückliche} with the anchor from slightly further down and also replace the anchor's text with the word from in the braces. This needs to be done generically (ie not hard-coded) as there are many.</p>

<p>I found that inserting an anchor into text in this particular way, was not really catered for in any jquery methods - hopefully I'm wrong.</p>

<p><strong>HTML given</strong></p>

<pre><code>&lt;tr&gt;
    &lt;td class="german"&gt;bin ich eine {glückliche} Fliege&lt;/td&gt;
    &lt;td class="english"&gt;am I a happy fly&lt;/td&gt;
    &lt;td&gt;
        &lt;div class="lessonLink"&gt;
            &lt;a href="http://www.german.org/happy" title="happy"&gt;&amp;gt;&amp;gt;&lt;/a&gt;
        &lt;/div&gt;
    &lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p><strong>HTML desired</strong></p>

<pre><code>&lt;tr&gt;
    &lt;td class="german"&gt;bin ich eine &lt;a href="http://www.german.org/happy" title="happy"&gt;glückliche&lt;/a&gt; Fliege&lt;/td&gt;
    &lt;td class="english"&gt;am I a happy fly&lt;/td&gt;
    &lt;td&gt;
        &lt;div class="lessonLink"&gt;
            &lt;a href="http://www.german.org/happy" title="happy"&gt;&amp;gt;&amp;gt;&lt;/a&gt;
        &lt;/div&gt;
    &lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p><strong>The jquery</strong></p>

<pre><code>$(function() {
    $('td.german').each(function() {

        var sentence = $(this).text();

        //get the squigglied text
        var startBrace = sentence.indexOf('{');
        var endBrace = sentence.indexOf('}');
        if (startBrace === -1 || endBrace === -1) return;
        var toReplace = sentence.slice(startBrace, endBrace+1);

        // find the anchor and replace it's text with squigglied
        var $newAnchor = $(this).siblings().find('a').first().clone();
        $newAnchor.text(toReplace.slice(1, toReplace.length-1));

        //take the new anchor and insert it into the main sentence
        // can I get full-string from DOM object? string concat looks vomit
        var stringToInsert = '&lt;a href="' + $newAnchor[0].href + '" ' +
                             'title="' + $newAnchor[0].title + '" &gt;' +
                              $newAnchor[0].text + '&lt;/a&gt;';

        var newSentence = sentence.replace(toReplace, stringToInsert);
        $(this).html(newSentence);
    });
});
</code></pre>
