MHowey
MHowey

Reputation: 681

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

Upvotes: 0

Views: 163

Answers (3)

NiematojakTomasz
NiematojakTomasz

Reputation: 2473

Or:

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

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

Upvotes: 2

Kevin Pei
Kevin Pei

Reputation: 5872

use

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

so html gets passed over.

Upvotes: 3

Morgan Delaney
Morgan Delaney

Reputation: 2439

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

Upvotes: 4

Related Questions