Solved

Strip div and span tags

Posted on 2004-08-17
5
399 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now