Hide text between javascript keys

Asked

Viewed 81 times

0

How can I hide a text between keys in my div

Ex:

<div> Hello {World} </div>
  • 2

    Do you know regular expression? It’s one of the ways to do this...

  • Add a span tag around the text you want hidden by inserting an id or class, and then just manipulate the text with JS. Example: <div> Hello <span id="text">{World}</span> </div>

  • It would be so easy, but I can’t edit this div :(

3 answers

1

Hello, try that regular expression:

let getDiv = document.querySelector('.teste').innerHTML;

let newTeste = getDiv.replace(/(\s\{.*?\})/, '');

document.querySelector('.teste').innerHTML = newTeste;
<div class="teste">Hello {World} !</div>

1


Using Regular Expression

var text = $("div").text();
$("div").html('').append(text.replace(/(\s\{.*?\})/, ''))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> Hello {World} </div>

  • Hi Netinho, great helped a lot! and if I want to replicate for several Ivs?

  • @Josimara, if your Divs are the same way as the question’s div, you can use Jquery.each() to scan the Divs. I made an example, if you want I edit the answer. See the example running here: https://jsfiddle.net/1p5m48wq/3/

  • That’s really great, thanks!

0

Take text from div, replace using regular expression

var textoDaDiv = $("div").text();  

$("div").text(textoDaDiv.replace(/ *\{[^)]*\} */g, ""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> Hello {World} </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.