replace plain-text with html using jQuery -


i'm trying write replacewith function in jquery replace specific words html.

basically i'm trying replace [this] <span class='myclass'> document loaded. i've looked around couple samples couple things saw unable work. i'm still learning jquery i'm hoping give me couple suggestions try.

thanks in advance, steven.

html/text:

[this]hello world![/this]​ 

javascript:

$(document).ready(function() {          // using replace      $("body").text().replace(/[this]/g,'<b>') });​ 

--edit--

i've made few changes function using nir's demo below. heres few more details i'm doing. i'm trying make inline fraction, have the appropriate classes , whatnot made. works when in html, when run jquery function doesn't seem work.

it's obvious tags replaced, css doesnt seem working mentioned.

heres link html here

and heres link jsfiddle here

$(document).ready(function() {      // using replace var text = $('body').text(); $("body").html(text.replace(/\[math\]/g,'<span class="container">').replace(/\[\/math\]/g,'</span>'));  var textt = $('body').text(); $("body").html(textt.replace(/\[top\]/g,'<div class="containme">').replace(/\[\/top\]/g,'</div>'));  var textl = $('body').text(); $("body").html(textl.replace(/\[line\]/g,'<div class="borderme">&nbsp;</div>'));  var textb = $('body').text(); $("body").html(textb.replace(/\[bot\]/g,'<div class="containme2">').replace(/\[\/bot\]/g,'</div>'));  });​ 

this html

 <span class="readme">whats up, here's fraction.&nbsp; <span class="container">     <div class="containme">1</div>     <div class="borderme">&nbsp;</div>     <div class="containme2">2</div>     </span>  &nbsp;&nbsp;hello? whats up, here's fraction.&nbsp;     <span class="container">     <div class="containme">1</div>     <div class="borderme">&nbsp;</div>     <div class="containme2">2</div>   </span>   &nbsp;&nbsp;hello? whats up, here's fraction.&nbsp;   </span> 

to shortend markup

<span class="readme"> whats up, here's fraction. [math][top]1[/top][line][bot]2[/bot][/math] </span> 

javascript replace() returns new string, not alter original string, should be:

$(document).ready(function() {         var content = $("body").text().replace(/\[this\]/g,'<b>')     $("body").html(content); });​ 

notice brackets have escaped, otherwise replace each character inside brackets <b>.

here's fiddle


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -