0

So guys, I just want to format a string with jQuery, the string can be with closed html tags <div><h2></h2><span></span></div> or without it such as <div><h2></h2><span>, or it may contains images such as <img src=""/>, but I need to format it to have as result only plain text, I suppose that this can be done with a regex, I googled it, but could not find such an example.

So if you could help me, I'll be very thankful for help. Thanks

An exemple of the string:

<p> 
    <span style="color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 20px; ">Преимущества стоматологической установок Anthos</span>
</p> 
<ul class="tech_list" style="margin: 0px; padding-right: 0px; padding-left: 0px; list-style-type: none; color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; "> 
    <li style="margin: 0px; padding: 8px 0px; font-size: 13px; line-height: 18px; overflow: hidden; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); "> 
       <img alt="Anthos " class="photo" src="http
0

4 Answers 4

4
var body;

try {
    body = document.implementation.createHTMLDocument().body;
    //In chrome this avoids requesting images in the html
}
catch(e){
    body = document.createElement("body");
}
//this doesn't execute css or scripts
body.innerHTML = '<p> <span style="color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 20px; ">Преимущества стоматологической установок Anthos</span></p> <ul class="tech_list" style="margin: 0px; padding-right: 0px; padding-left: 0px; list-style-type: none; color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; "> <li style="margin: 0px; padding: 8px 0px; font-size: 13px; line-height: 18px; overflow: hidden; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); "> <img alt="Anthos " class="photo" src="http'

console.log( $(body).text() );
//Преимущества стоматологической установок Anthos  

demo http://jsfiddle.net/FvBzX/

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, but not working, I updated my post with an example of text.
@DenisHoss works fine jsfiddle.net/FvBzX, you are doing something differently ?
Yeap in jsfiddle works fine, I don't understand where I'm doing it wrong
2

Try

$(".example_1").html( $(".example_1").text() );

Take the HTML of the div, grab the text of the div and place it back into the html of the outside containers.

Stolen from Googled Website

2 Comments

This will execute scripts in the html. In chrome it will also request images.
Your solution working good, but, in some texts there are a little errors, so far your solution it's the best. I updated my post with an example of text.
1

I guess this is what you're looking for:

https://stackoverflow.com/a/653699/1456376

jQuery.fn.stripTags = function() { return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') ); };

1 Comment

Sorry, but not working, I updated my post with an example of text.
0

this RegEx should strip all html tags from a string:

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

1 Comment

Sorry, but not working, I updated my post with an example of text.

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.