Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping attributes from HTML - regex

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
I'm working with this app and need to post the contents of js-generated HTML for serverside processing. The js functions add a bunch of attributes for some dynamic sorting and style.

Obviously I don't want to strip all attributes since there are some persistent attributes for images, etc.

I guess for this particular app, the table tags are all that need to be cleaned.

Any insights before I go muddling with regex?

Code:
<table>
<tbody class="ui-sortable" style="position: relative;"><tr style="background-color: transparent;"><th>Alternates</th><th>Lead Roles</th><th>Work Phone</th><th>Cell Number</th><th>Home Number</th><th> </th></tr></tbody><tbody class="ui-sortable" style="position: relative;"><tr style="background-color: rgb(225, 225, 225);"><th colspan="6">FOECC MANAGER</th></tr><tr style="background-color: transparent;">
<td style="background-color: transparent;">2nd</td>
<td style="background-color: transparent;">TEST NAME</td>
<td style="background-color: transparent;"> </td>
<td style="background-color: transparent;"> </td>
<td style="background-color: transparent;"> </td>
<td style="background-color: transparent;"><img width="13" height="15" class="delete" alt="Delete" src="../_images/delete.png" /></td></tr><tr style="background-color: rgb(225, 225, 225);">
 
Hi

This regular expression will remove all attributes of the tags :
Code:
stripped=original.replace(/(<\w+).*?(\/?>)/g,'$1$2')
But probably you only want to remove all [tt]class[/tt] and [tt]style[/tt] attributes :
Code:
stripped=original.replace(/\s+(class|style)=".*?"/g,'')

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top