Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide special symbols in a <p> tag

For example we have

<p>Hey My Name is Thalapathy. I live in +++America+++. </p>

In here we want to hide +++ . Result must look like

My Name is Thalapathy. I live in America

like image 824
mithelan Avatar asked Dec 02 '25 11:12

mithelan


2 Answers

find in all the elements if this +++ occurs and use regex to replace whatever you want, something like this. and it will work on all the elements in body, where ever this +++, 3 plus will occur, it will be removed.

$("body").children().each(function () {
    $(this).html( $(this).html().replace(/\+\+\+/g,"") );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Hey My Name is Thalapathy. I live in +++America+++. </p>

and if you don't want to use jQuery then go for this. use Treewalker for the same.

const treeWalker = document.createTreeWalker(document.body);
  while (treeWalker.nextNode()) {
    const node = treeWalker.currentNode;
    node.textContent = node.textContent.replace(/\+\+\+/g, '');
  }
<p>Hey My Name is Thalapathy. I live in +++America+++. </p>
like image 95
Atul Rajput Avatar answered Dec 03 '25 23:12

Atul Rajput


If the characters are known and constant (always the same)- then you can do a simple replace - note that you will need to escape the characters. Also note the "g" in the regex / replace - this will allow the replacement globally in the string.

const text="Hey My Name is Thalapathy. I live in +++America+++."
const newText = text.replace(/\+\+\+/g,'');
document.querySelector("#result").textContent = newText;
<p id="result"></p>
like image 22
gavgrif Avatar answered Dec 04 '25 01:12

gavgrif