Solved

Strip div and span tags

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

827 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