jQuery 仅在不存在时才替换字符
我想替换所有 "<"和“>”与“[”和“]”
,但我不想触摸/替换所有“
”或“
”
$('.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);
});
i would like to replace all the "<" and ">" with "[" and "]"
but i want to not touch/replace all the "<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);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更简单的方法可能是分两步处理。
首先,将所有
<
和>
替换为[
和]
。其次,将
[br]
和[/br]
替换为
和
>。An easier way to do this is probably to handle it in two steps.
Firstly, replace all
<
and>
with[
and]
.Secondly, replace
[br]
and[/br]
back with<br>
and</br>
.首先将
替换为 fakebropen然后将
替换为 fakebrclosewysiwyg_val = wysiwyg_val.replace("
","fakebropen").replace("
","fakebrclose" );
//然后进行操作
wysiwyg_val = wysiwyg_val.replace("fakebropen","
").replace("fakebrclose","
");
first replace
<br>
with fakebropenthen replace
</br>
with fakebrclosewysiwyg_val = wysiwyg_val.replace("<br>","fakebropen").replace("</br>","fakebrclose");
//make your operations then
wysiwyg_val = wysiwyg_val.replace("fakebropen","<br>").replace("fakebrclose","</br>");