开发者

jQuery dynamic replacement with RegEx

开发者 https://www.devze.com 2023-04-10 07:26 出处:网络
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:

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">');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号