Solved

Strip div and span tags

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP:Radiobuttonlist/asp:RadioButtonListItem custom styling 1 42
Delay Form Submission without touching existing code? 5 24
where is team.js? 3 26
Javascript Form Change 5 23
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…
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…

810 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