jQuery dynamic replacement with RegEx
If i have {code}{type="hey"}
and I want to replace it with <code><pre class="hey">
how can I do it binding some method to this function:
$('.wysiwyg textarea').live('keyup',function(){
wysiwyg_val = $(this).val();
wysiwyg_val = wysiwyg_val
.replace(/\n/g, "<br />")
.replace(/\n\n+/g, '<br /><br />')
.replace(/\{code\}/g,"<pre><code>")
.replace(/\{\/code\}/g,"</code></pre>")
.replace(/\{code}/g,"</code></pre>")
.replace(/\{img\}/g,'<img src="http://localhost/CI_DEVBASE/img/logo.png" width="150" height="50"开发者_StackOverflow')
.replace(/\{\/img\}/g,'/>');
$('.wysiwyg-preview').html(wysiwyg_val);
});
Note that I just replace {code}
with <code><pre>
. I would like to replace also in case of {code}{type="$somenthing"}
to <code><pre class="$somenthing">
where $somenthing
is a dynamic parameter, not static.
Try adding this line to the end of your replace statement -
.replace(/\{code\}\{type="(.*)"\}/g, '<code><pre class="$1">');
精彩评论