... and nice to see you back ;-)
Main Topics
Browse All TopicsHi all experts !!
I'm looking for a regexp which will strip all attributes except
- colspan and
- class
from <td> tags.
example:
<td width="100" class="myClass" rowspan="3" colspan="4">My tag</td>
should return
<td class="myClass" colspan="4">My tag</td>
Thank you in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thank you Zvonko :)
We're almost there. This regexp works when the td contains both a "class" and a "colspan" attribute.
I need it to work when there's none or only one attribute present.
examples:
1)
<td width="195">
becomes
<td>
2)
<td valign="top" class="myClass" width="394" colspan="2">
becomes
<td class="myClass" colspan="2" >
and
3)
<td valign="top" width="394" colspan="2">
becomes
<td colspan="2">
Before another regexp guro notifies the upper regexp limitations here my update:
<script>
var myHTML = '<tr><td width="100" colspan="4" class="myClass" rowspan="3" >My tag</td></tr>';
var newHTML = myHTML.replace(/<td[^>]*?(
function(m){
var pClass = m.match(/class=["']?\w+["'
var pCol = m.match(/colspan=["']?\d+[
return "<td "+(pClass?pClass:"")+" "+(pCol?pCol:"")+" >";
});
alert(newHTML);
</script>
Business Accounts
Answer for Membership
by: ZvonkoPosted on 2007-07-04 at 04:16:45ID: 19417805
How about this:
class=["'] ?\w+["']?) [^>]*?(col span=["']? \d+["']?)[ ^>]*?>/i," <td $1 $2 >");
<script>
var myHTML = '<tr><td width="100" class="myClass" rowspan="3" colspan="4">My tag</td></tr>';
var newHTML = myHTML.replace(/<td[^>]*?(
alert(newHTML);
</script>