<p>I am trying to write some JavaScript RegEx to replace user inputed tags with real html tags, so <code>[b]</code> will become <code>&lt;b&gt;</code> and so forth. the RegEx I am using looks like so</p>

<pre><code>var exptags = /\[(b|u|i|s|center|code){1}]((.){1,}?)\[\/(\1){1}]/ig;
</code></pre>

<p>with the following JavaScript</p>

<pre><code>s.replace(exptags,"&lt;$1&gt;$2&lt;/$1&gt;");
</code></pre>

<p>this works fine for single nested tags, for example:</p>

<pre><code>[b]hello[/b] [u]world[/u]
</code></pre>

<p>but if the tags are nested inside each other it will only match the outer tags, for example </p>

<pre><code>[b]foo [u]to the[/u] bar[/b]
</code></pre>

<p>this will only match the <code>b</code> tags. how can I fix this? should i just loop until the starting string is the same as the outcome? I have a feeling that the <code>((.){1,}?)</code> patten is wrong also?</p>

<p>Thanks</p>

AFAIK you can't express recursion with regular expressions.
You can however do that with .NET's System.Text.RegularExpressions using balanced matching. See more here:
http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx
If you're using .NET you can probably implement what you need with a callback. 
If not, you may have to roll your own little javascript parser.
Then again, if you can afford to hit the server you can use the full parser. :)
What do you need this for, anyway? If it is for anything other than a preview I highly recommend doing the processing server-side.