简体   繁体   English

如何使用Jquery Javascript替换

[英]How to Use Jquery Javascript Replace

I'm using this in my wysiwyg to replace 我在所见即所得中使用它替换

< pre> Hi my name is< /pre>

with

< div id="precode">Hi my name is< /div>

This is the working code code 这是工作代码

v=v.replace(/<pre>(.*)<\/pre>/gim,'<div id="precode">$1</div>');

This works fine unless the string contains a < br> 除非字符串包含<br>

EDIT This is the code 编辑这是代码

if(div){div.innerHTML=this.obj.toggletext||'Spell Check'}
$('#spellcheck_words').slideToggle('slow', function() { }); 
if(this.xhtml&&!this.ie){
v=v.replace(/<strong>(.*)<\/strong>/gi,'<span style="font-weight: bold;">$1</span>');               
v=v.replace(/(<img [^>]+[^\/])>/gi,'$1/>')
v=v.replace(/<em>(.*)<\/em>/gi,'<span style="font-weight: italic;">$1</span>')
}

EDIT 编辑

$('pre').replaceWith('<div>'+$(this).html()+'</div>');

use 采用

$('pre').replaceWith('<div>'+$(this).html()+'</div>');

so html gets passed over. 所以html被传递了。

Or: 要么:

v=v.replace(/<pre>((?:.|[\r\n])*)<\/pre>/gim,'<div id="precode">$1</div>');

Problem is not with <br/> , but with new-line characters. 问题不在于<br/> ,而是换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM