Need help doing a Find and Replace procedures in JavaScript

wxdqz

New Member
Hello, I have constructed javascript code that half works. For example if I have this html code:

<table width="3"><tbody><tr><td width="3">hello</td></tr></tbody></table>

My javascript's main goal is to go thru the inputed html code which in my javascript is under the variable strTextData and find certain tags (in this case <td tag) and remove attributes that are not allowed (in this case width=). My code works if there are no tags with the width attribute in them, for example:

<table><tbody><tr><td width="3">hello</td></tr></tbody></table>

(the table tag does not have a width, but if it does, see first example, then my code won't work)

The reason is that I have the code using indexOf, it finds the indexOf <td tag and the indexOf the width= tag and sets them as variables a and c respectivel. If c comes after a then my code will delete the width, but if it comes before, for example like in a different tag like table, where width is allowd then it won't touch it. How do I make it find the width= attribute in the right spot?? without stopping at places where it is allowed?? Here is my code:

// external command for Cleaning (Salman)
if ("jsclean" == strCmdName)
{
var strTextData = eWebEditPro [sEditorName].getSelectedHTML();
alert(strTextData);

/* Tag: TD, Attribute: WIDTH */


if((strTextData.indexOf("<td ")!=-1) && (strTextData.indexOf("width=")!=-1))
{
var a = strTextData.indexOf("<td ");
var c = strTextData.indexOf("width=");

if (a<c)
{
var strTextData=strTextData.replace(/width=/i,"");
}

}

var theEditor = eWebEditPro[sEditorName];
eWebEditPro[sEditorName].pasteHTML(strTextData);

}

}

I appreciate anyone who can help me, thank you!
 
Back
Top