简体   繁体   中英

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>

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.

Or:

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

Problem is not with <br/> , but with new-line characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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