Solved

Strip div and span tags

Posted on 2004-08-17
5
410 Views
Last Modified: 2012-05-05
I need a function to strip DIV and SPAN tags (and their properties) from a string.

Exampl:

<div class=something><span class=else> sample text </span></div>

Should return:
sample text

Thank you.
0
Comment
Question by:Esopo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:darksinclair
ID: 11825480
So.. you have a page that has alot of convoluted code I presume and you want to automatically have something search through your code and remove any instance of <div class></div> and <span></span> ??

Do you want this to happen only once or every time the page loads?  Couldnt' you use the search and replace on any popular WYSIWYG Editor?  Or do you have many many pages that this would take forever?

Cheers,
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11825485
Like this:

<script>

var myString = "Some Text: <div class=something><span class=else> sample text </span></div>, <b>more</b> text.";


alert(myString.replace(/<\/?(div|span)[^>]*>/g,""));

</script>

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 11825497
Sorry, better is this:

<script>

var myString = "Some Text: <div class=something><span class=else> sample text </span></div>, <b>more</b> text.";


alert(myString.replace(/<\/?(div|span)[^>]*>/gi,""));

</script>

0
 
LVL 14

Author Comment

by:Esopo
ID: 11825607
darksinclair,

No, actually is to clean user input.

Zvonko,

Thanks, worked like a charm!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11825910
You are welcome.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question