Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript - regex to remove html elements

  • 14-08-2007 7:13am
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    Hi,

    I have the following example code below
    <html>
     <table>
       <tr>
         <td>Cell 1</td>
         <td>Cell 2</td>
         <td>Cell 3</td>
       </tr>
     <table>
    </html>
    

    I dont have control over the table html that is written out but i need to remove all html tags relating to the table (i.e. <table>,<tr>,<td>) I also need to preserve the contents of the text in the <td> tags. I can then format the text with css

    I tried using the javascript match method with a regex but i think the < and > symbols are causing problems in the regex.

    Does anyone know how i can search and remove html tags with a regex ?

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    Do you have to use a regex...

    Can you not just use

    document.getElementsByTagName('td').item(i)

    where i is the index var


  • Registered Users, Registered Users 2 Posts: 872 ✭✭✭grahamor


    OK, maybe i could use that, but how would i remove that element and preserve it's contents ?

    I know using the regex is a bit of a hack but i cant think of any other way to do it.

    Thanks


  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    The method above should give you the content less the tags... is that not what you are looking for?


  • Registered Users, Registered Users 2 Posts: 872 ✭✭✭grahamor


    Yes i want the content in the tags but i also want the table,tr,td tags completly removed so they dont affect layout !


  • Registered Users, Registered Users 2 Posts: 614 ✭✭✭dent


    function getTableRows(tableId)
    {
        var table = document.getElementById(tableId);
        var tableBody = table.getElementsByTagName("tbody")[0];
        var rows = tableBody.getElementsByTagName("TR");
        return rows;
    }
    
    function getTableColumns(tableId)
    {
        var table = document.getElementById(tableId);
        var tableBody = table.getElementsByTagName("tbody")[0];
        var columns = tableBody.getElementsByTagName("TD");
        return columns;
        
    }
    
    function getTableContent(tableId,rowNum,columnNum)
    {
        var table = document.getElementById(tableId);
        var tableBody = table.getElementsByTagName("tbody")[0];
        var row = table.getElementsByTagName("TR")[rowNum];
        var cell = row.getElementsByTagName("TD")[columnNum];
        var myceltext=cell.childNodes[0];
        return myceltext.data
    }
    
    function alertColumnsRows(tableId)
    {
        var rows = getTableRows(tableId).length;
        var columns = getTableColumns(tableId).length / rows;
        
        for(var i = 0; i < rows; i++)
        {
            for(var x = 0; x < columns; x++)
            {
                alert(getTableContent(tableId,i,x));
            }
        }
    }
    

    Calling alertColumnsRows(tableId) will alert the contents of the table, all you would have to do is append the content in a variable with whatever html tags you want.

    You could then write that to the screen and kill the table.

    (Note on above code, pass in element returned from getElementById() as this will improve performance)


  • Advertisement
Advertisement