30

I have this code :

var content = "<p>Dear sms,</p><p>This is a test notification for push message from center II.</p>";

and I want to remove all <p> and </p> tags from the above string. Just want to display the string values without html tags like :

"Dear sms, This is a test notification for push message from center II."
3
  • I don't think it is duplicate. because it is tagged under Javascript too. Commented Dec 17, 2012 at 9:49
  • I holding that html string in a javascript varibale. I want to remove those tags from that variable Commented Dec 17, 2012 at 9:52
  • Duplicate of this: stackoverflow.com/questions/822452/… Commented Dec 17, 2012 at 9:53

12 Answers 12

65

Why not just let jQuery do it?

var content = "<p>Dear sms,</p><p>This is a test notification for push message from center II.</p>";

var text = $(content).text();
Sign up to request clarification or add additional context in comments.

3 Comments

Keep in mind that if content has something like &lt;div&gt; it will become a tag after this code is run
This does not remove a <br/>
@PHPMaster5000: Yes it does. What does your HTML look like?
32

Here is my solution ,

function removeTags(){
    var txt = document.getElementById('myString').value;
    var rex = /(<([^>]+)>)/ig;
    alert(txt.replace(rex , ""));

}

4 Comments

thanks man, this is the best answer, because its pure javascript and your regexp handles all the html tags
An improvement to this regex would be: (<([^>]+)>)|(&lt;([^>]+)&gt;) This will remove standard html tags as well as any escaped tags.
Please don't process HTML using regular expressions. It is a mistake and you will likely get burned badly for it. HTML is not a regular language and a regular expression, even one thousands of characters long, is not sufficient for it. You need a real parser, or utilize functions of the browser/DOM. I as an attacker could easily get HTML past both of these examples.
regex to remove &nbsp as well ?
9

Using plain javascript :

content = content.replace(/(<p>|<\/p>)/g, "");

3 Comments

What about whitespace and/or attributes? For example <p class="bla"></p >
Answers the OP exact question, but if the tags have attributes the regex would need to be /(<p[^>]*>|<\/p>)/g
Here, the variable seems to be generated and I think it is easy to know which elements we have to remove... Even, if it is a bit more complicated, I think that this method will work : it is a basis on the question asked and can be easily adapted after.
4

You can use jQuery text() to get plain text without html tags.

Live Demo

withoutP = $(content).text()

Comments

1
var content = "a<br />";
var withoutP = $(content).text()
alert(withoutP )

This one does not work for the .text() solution.

Comments

1

this one is checking for special chars

var $string = '&lt;a href=&quot;link&quot;&gt;aaa&lt;/a&gt;';
var $string2 = '<a href="link">aaa</a>';
var $string3 = 'BBBBB';
var $string4 = 'partial<script';
var $string5 = 'has    spaces';

function StripTags(string) {

  var decoded_string = $("<div/>").html(string).text();
  return $("<div/>").html(decoded_string).text();

}

console.log(StripTags($string));
console.log(StripTags($string2));
console.log(StripTags($string3));
console.log(StripTags($string4));
console.log(StripTags($string5));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Comments

1

myString.replace(/<[^>]*>?/gm, '');

Comments

0

Place hidden element in markup, or create it from jQuery. Use this code to get plain text without complications with incomplete tags, &lt; etc.

content = $(hiddenElement).html($(hiddenElement).html(content).text()).text();

Comments

0

Above approch is not working for me. so i got one alternative solution to solve this issue`

var regex = "/<(.|\n)*?>/";
var originaltext = "1. Males and females were compared in terms of time    management ability. <br><br>The independent variables were the people in the   study.<br><br> Is this statement correct";
var resultText = body.replace(regex, "");
console.log(result);

Comments

0

Use regex:

var cleanContent = content.replace(/(<([^>]+)>)/ig,"");

Comments

0

you can use striptags module to remove HTML and get the text. It's quite an easy and straightforward solution.

Comments

-3
function htmlTagremove(text) {

   var cont = document.createElement('div'),
   cont.innerHTML=text;

   return $(cont).text();

}

htmlTagremove('<p><html>test');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.