PDA

View Full Version : javascript string stripping


Vege
04-29-2006, 12:22 PM
if i have a string

var string = "dasdsa<h1></h1><h2>big</h2>dsa";

i need to strip all tags away from the string that have no content, in this case <h1></h1>

but can this be done if tag names are unknown and different tags occure?
example

<span></span>
should also be removed becouse it's "empty"

im kinda nood with javascript string comparisons.

_Aerospace_Eng_
04-29-2006, 12:23 PM
Umm this is the CSS forum not javascript forum. Javascript is clientside. Moving you there.

Jon Hanlon
04-29-2006, 08:51 PM
You can do it using regular expressions and the replace method.
Make use of backreferences.

Vege
04-30-2006, 03:45 AM
my_string = my_string.replace( /< (.*)> <\/\1>/g, '' );
This didint solve my problems, but here it is for reference.

¥åßßå
04-30-2006, 05:33 AM
You have some spaces in your regex, this worked for me.

<script type="text/javascript">
var my_string = "dasdsa<h1></h1><h2>big</h2>dsa<span></span>";
my_string = my_string.replace( /\<(.*)>\<\/\1>/g, '' );

window.alert( my_string );
</script>


¥

Vege
04-30-2006, 05:42 AM
Sry, little misunderstood here.

The orginal regexp fixed my problem as it was used also without the space as you suggested.
Point of the message was that the regexp was not the method i needed afterall, but thats another story and not to be told here.

¥åßßå
04-30-2006, 05:50 AM
No problem ;)

¥