replace plain-text with html using jQuery
Asked Answered
U

3

9

I'm trying to write a replacewith function in jQuery that will replace specific words with html.

Basically I'm trying to replace something like [this] with <span class='myclass'> as the document is loaded. I've looked around for a couple samples but the couple things I saw I was unable to get to work. I'm still learning jQuery so I'm hoping someone could give me a couple suggestions as what to try.

Thanks in advance, Steven.

HTML/text:

[this]hello world![/this]​

JavaScript:

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

--EDIT--

I've made a few changes to the function using Nir's demo below. Heres a few more details about what I'm doing. I'm trying to make an inline fraction, which I already have the the appropriate classes and whatnot made. It works well when in html, but when I run the jQuery function It doesn't seem to work.

It's obvious the tags are replaced, but my css doesnt really seem to be working as I mentioned.

Heres a link to the HTML HERE

and heres a link to the jsfiddle HERE

$(document).ready(function() {    

// Using just 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 a 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 a 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 a fraction.&nbsp;
  </span>

to this shortend markup

<span class="readme"> Whats up, here's a fraction. [math][top]1[/top][line][bot]2[/bot][/math] </span>
Uncharitable answered 3/7, 2012 at 20:55 Comment(2)
How does the string you're looking for influence the expected output? And you're just inserting an opening tag? What about the closing tag for that inserted element?Perdita
(a) You did not set up your fiddle to use jQuery (b) .replace returns a new string: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…Sensorium
E
8

javascript replace() returns a new string, it does not alter the original string, so it should probably be:

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

Notice that the brackets will have to be escaped, otherwise it will replace each character inside the brackets with <b>.

Here's a FIDDLE

Evite answered 3/7, 2012 at 21:10 Comment(2)
A (slightly-updated) JS Fiddle to avoid the second replace() in your linked-fiddle.Perdita
this is a great demo, thank you very much. I'm going to make a few edits to my OP. i tried to the answer from Nic before i saw yours and, it did infact replace how i needed, but it seems as though my css isnt working properly. could you take a look at this demo to give me a few suggestions? jsfiddle.net/stevenschemers/juTtG/28Uncharitable
F
2

here is a demo that uses regex:

$(document).ready(function() {    
    // Using just replace
    var text = $('body').text();
    $("body").html(
        text.replace(/\[this\]/g,'<b>').replace(/\[\/this\]/g,'</b>')
    );
});
Funke answered 3/7, 2012 at 21:6 Comment(3)
Please don't just link to the solution, also include the code in your answer. If the links becomes unavailable for whatever reason, your "answer" becomes useless.Sensorium
thank you for the demo. I added a few more lines in attempt to complete the function for what i'm trying to do, and it seems as though i broke it :|. would you mind looking at this fiddle? jsfiddle.net/stevenschemers/juTtG/24Uncharitable
I updated my OP with more information regarding what I'm trying to actually accomplish. Thank you for your helpUncharitable
O
0

As nobody explain way to replace text with custom HTML element now I use the next function:

replaceWithAnchor: function (el, text) {
        let anch = $('<span>');
        anch.attr('id', `my-link-1`);
        anch.text(text);

        var content = el.html().replace(text, '<span id="need-to-replace"></span>');
        el.html(content);
        el.find('#need-to-replace').replaceWith(anch);
    }
Overtrade answered 27/4, 2020 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.