jQuery只有在没有的情况下才能替换chars

时间:2011-10-06 14:17:05

标签: javascript jquery replace

我想替换所有"<" and ">" with "[" and "]",但我不想触及/替换所有"<br>" or "</br>"

$('.wysiwyg textarea').live('keyup',function(){

     wysiwyg_val = $(this).val();

    wysiwyg_val = wysiwyg_val
    .replace(/\n/g, "<br>")
    .replace(/\{code\}\{type="(.*)"\}/g, '<code><pre class="$1">')
    .replace(/\{img\}\{url="(.*)"\}/g, '<img src="$1"')
    .replace(/\{code\}/g,"<pre><code>")
    .replace(/\{\/code\}/g,"</code></pre>")
    .replace(/\{code}/g,"</code></pre>")
    .replace(/\{code\}\{(.*)}/g, '<code><pre class="$1">')
    .replace(/\{\/img\}/g,'/>');


    $('.wysiwyg-preview').html(wysiwyg_val);


  });

2 个答案:

答案 0 :(得分:1)

更简单的方法可能是分两步处理。

首先,将所有<>替换为[]

其次,将[br][/br]替换为<br></br>

答案 1 :(得分:1)

首先用fakebropen替换<br> 然后用fakebrclose

替换</br>

wysiwyg_val = wysiwyg_val.replace("<br>","fakebropen").replace("</br>","fakebrclose");

//然后进行操作

wysiwyg_val = wysiwyg_val.replace("fakebropen","<br>").replace("fakebrclose","</br>");