Home > javascript > javascript – using regular expression to remove certain tags from string

javascript – using regular expression to remove certain tags from string

From today, I am trying to post again on a regular basis :)

Here is a one line regular expression to remove a particular html tag from a string.

In the example given below I am stripping the span tags from the given string.

 <script language="javascript">
	linkText = '<a href="test.html"><span><span class="random">Hello World</span></span></a>'
	alert(linkText);
	linkText = linkText.replace(/<[\/]{0,1}(span|SPAN)[^><]*>/g,"");
	alert(linkText);
 </script>

Advertisement
  1. Girish
    October 6, 2010 at 6:51 pm | #1

    Thanks.
    Do u know how to relplace any tag with it’s content ?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.