67

I have some text like this:

<span>My text</span>

I want to display without tags:

My text

I also don't want to apply the tags, I want to strip them. What's an easy way to do that?

Angular html:

<div>{{myText | htmlToPlaintext}}</div>
4
  • You have tagged this under AngularJS, so I assume you want to do display the text using AngularJS. If so, is this text in a model? Which html tag are you using to display the contents of the model? Commented Jun 25, 2013 at 5:44
  • @callmekatootie I created a filter based on the answer Abhishek gave. Commented Jun 25, 2013 at 5:56
  • 2
    I see that and his solution uses jQuery - You have tagged this under AngularJS and I have a feeling that you you can do it in AngularJS without using jQuery, if only you can give more information in your question - particularly where you wish to display the text in your view... Commented Jun 25, 2013 at 6:16
  • @callmekatootie I added an example of what I'm doing in angular Commented Jun 25, 2013 at 7:05

8 Answers 8

209

jQuery is about 40 times SLOWER, please do not use jQuery for that simple task.

function htmlToPlaintext(text) {
  return text ? String(text).replace(/<[^>]+>/gm, '') : '';
}

usage :

var plain_text = htmlToPlaintext( your_html );

With angular.js :

angular.module('myApp.filters', []).
  filter('htmlToPlaintext', function() {
    return function(text) {
      return  text ? String(text).replace(/<[^>]+>/gm, '') : '';
    };
  }
);

use :

<div>{{myText | htmlToPlaintext}}</div>  
Sign up to request clarification or add additional context in comments.

13 Comments

I actually prefer String(text).replace(/<[^>]+>/gm, ''). I've been using it for years without any issues.
jQuery is 40 times slower, but you still get 8000 ops/s, so not using jQuery for that reason is premature optimization.
I would never add a jQuery dependency just to convert html to text, but if my project already used jQuery I would choose the jQuery solution because I consider a custom-written regular expression less readable, less tested and less reliable. Does your regular expression protect against all possible forms of XSS?
using the DOM engine covers a lot more cases that this simple replace those not example: some codes here &#146; &#147; &#148;
You should probably add a null-check in there, or it will output "null": return text ? String(text).replace(/<[^>]+>/gm, '') : "";
|
21

from https://docs.angularjs.org/api/ng/function/angular.element

angular.element

wraps a raw DOM element or HTML string as a jQuery element (If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite.")

So you simply could do:

angular.module('myApp.filters', []).
  filter('htmlToPlaintext', function() {
    return function(text) {
      return angular.element(text).text();
    }
  }
);

Usage:

<div>{{myText | htmlToPlaintext}}</div>

8 Comments

I would avoid this approach as well; might not take as long as full jQuery, but certainly much slower than accepted answer.
I suggest wrapping your text in an element otherwise a plain old string like "hi" will return an empty string angular.element('<div>'+text+'</div>').text();
if you're in the link function of a directive you can simply do element.text() 0.o
I prefer this solution to using a regex, at least in cases where speed isn't critical. It's more correct and will actually convert the html to plain text, extraneously less than signs, special entities and all.
How to show the preview ,instead of showing the plain text(it is working). but i need to show the preview like stack overflow shows while posting question or answer including images also
|
4
var app = angular.module('myapp', []);

app.filter('htmlToPlaintext', function()
{
    return function(text)
    {
        return  text ? String(text).replace(/<[^>]+>/gm, '') : '';
    };
});

<p>{{DetailblogList.description | htmlToPlaintext}}</p>

4 Comments

add some formating an some insights to the code, that helps to generate better answers.
Please add some explanation. Your answer is currently flagged "low quality" and might eventually be removed.
There's a difference between a low-quality answer that should be voted down and a non-answer that should be deleted. Please see You're doing it wrong: A plea for sanity in the Low Quality Posts queue.
How to show the preview ,instead of showing the plain text(it is working). but i need to show the preview like stack overflow shows while posting question or answer including images also
3

You want to use the built-in browser HTML strip for that instead of applying yourself a regexp. It is more secure since the ever green browser does the work for you.

angular.module('myApp.filters', []).
  filter('htmlToPlaintext', function() {
    return function(text) {
      return stripHtml(text);
    };
  }
);

var stripHtml = (function () {
  var tmpEl = $document[0].createElement("DIV");
  function strip(html) {
    if (!html) {
      return "";
    }
    tmpEl.innerHTML = html;
    return tmpEl.textContent || tmpEl.innerText || "";
  }
  return strip;
}());

The reason for wrapping it in an self-executing function is for reusing the element creation.

2 Comments

How to show the preview ,instead of showing the plain text(it is working). but i need to show the preview like stack overflow shows while posting question or answer including images also
changed $document[0] to document and it worked for me! Thank you
3

<div ng-bind-html="myText"></div> No need to put into html {{}} interpolation tags like you did {{myText}}.

and don't forget to use ngSanitize in module like e.g. var app = angular.module("myApp", ['ngSanitize']);

and add its cdn dependency in index.html page https://cdnjs.com/libraries/angular-sanitize

Comments

0

You can use ng-bind-html, don't forget to inject $sanitize service into your module Hope it helps

Comments

-3

Use ng-bind-html this is only proper and simplest way

1 Comment

Welcome to Stack Overflow. Please add more details for clarification. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the OP.
-7

Use this function like

 String.prototype.text=function(){
   return this ? String(this).replace(/<[^>]+>/gm, '') : '';
 }

  "<span>My text</span>".text()
  output:
  My text

Fiddle

4 Comments

This is a jQuery-based solution. The question was about angularjs.
Rev The mark answer is javascript and if I did it using jquery what make it difference
Your solution is not compatible with OP's requirements. he already built his app on angularjs. jquery is a different framework.
+1 and I don't see here any jQuery... wtf? Another problem is @Abjo's method extends build in String prototype. That some developers choose to be angry about.

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.