Solved

Strip div and span tags

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now