-3

Possible Duplicate:
Strip HTML from Text JavaScript

Is there a way to remove html from a string in Javascript and keep the : (colon)

I have gone through Strip HTML from Text JavaScript and how do you strip html tags in textarea input and some similar questions on SO, but unable to find the right solution.

For e.g. "Something here<div> content</div>" should give "Something here content" and "Something here :)" should give "Something here :)"

The offered solutions in other problems help in stripping the HTML content but also strip smileys.

EDIT: Before mentioning solutions which would strip smileys from the text, please note that I need this in improving my chat engine solution at www.chesshunt.com. Where currently I am stripping the HTML tags along with the smileys

My current code

function strip(html) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    return tmp.textContent || tmp.innerText;
    }
11
  • Please show the code you have tried that didn't work as expected.
    – jrummell
    Oct 17, 2012 at 19:16
  • 1
    That very first SO post you linked to works just fine jsfiddle.net/QXBZe It doesn't make sense that HTML stripping would ever remove a text colon or semicolon.
    – Alex Wayne
    Oct 17, 2012 at 19:18
  • 2
    @Anshu Why do you think so? jsfiddle.net/KZM2S
    – Ram
    Oct 17, 2012 at 19:21
  • 1
    @Anshu Well then maybe you should post the code you are currently using that is acting up? Because you already linked to solutions that should be working for this task. Therefore, it's your code that has an error that we will happily help you find. But we probably wont be signing up on your website just so we can sift through your whole codebase and find a small eror in a tiny part of it. Just post the code you are currently using to do this escaping.
    – Alex Wayne
    Oct 17, 2012 at 19:26
  • 1
    : is a colon. ; is a semicolon. Just saying... Oct 17, 2012 at 19:27

6 Answers 6

2

I have to disagree with your statement that "The offered solutions in other problems help in stripping the html content but also strip smileys."

Here is an example based on your links: http://jsfiddle.net/be8gz/

4
  • I really have no idea.. how it is not working for me. Probably I have been banging my head somewhere else.
    – Anshu
    Oct 17, 2012 at 19:32
  • @Anshu the issue might be upstream with your html string.
    – Christophe
    Oct 17, 2012 at 19:35
  • Aww, it breaks one of my favourite emoticons: >_< Oct 17, 2012 at 19:36
  • Wow, @Christophe You are Amazing! This issue took a lot of time. But Your answer is very useful to me. You saved me. Thank you So Much Jan 2, 2014 at 20:00
2

Assuming the content is in another container just use the .text() function:

$('#some_container').text();
1
  • I already mentioned I have tried it. It would not retain smileys and I need that
    – Anshu
    Oct 17, 2012 at 19:22
2

You could create an element using

var element = document.createNode('<div>'+yourString+'</div>');

and then get the textContent (for all browsers except IE) or innerText.

EDIT : Arg, there wasn't jQuery initially in the tags... So it's possible to use

var stripped = $('<div>'+yourString+'</div>').text();

DEMONSTRATION (keeping the smiley)

1
  • This is the Best Answer. Jan 2, 2014 at 20:05
0

A simple regex should do the job

var string = "Something here<div> content</div> :)";
alert(string.replace(/<([^>]+)>.*?<\/\1>/g,''));​
2
  • not sure why this was down voted, it does the job....
    – Rene Koch
    Oct 17, 2012 at 19:25
  • I am using this expression and I never faced problem. I collect data from different sites feed
    – Anoop
    Oct 17, 2012 at 19:42
0

You can use regular expression jsfiddle

    var myStr = "Something here<div> content</div>";    
    var myRegEx = /<\/?[^<>]*\/?>/gim;
     var result = myStr.split(myRegEx).join("");

OR

    result = myStr.replace(/<\/?[^<>]*\/?>/gim, "");

​
1
0

I don't understand if you want to keep the smileys or not. Based on the example in your question I must assume you want to keep the smileys, but get rid of any html tags. I think a regex like this would work:

pattern = /<.*?>/g;

See the JSFiddle.

Not the answer you're looking for? Browse other questions tagged or ask your own question.