Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regex: Match text NOT part of a HTML tag

I would really want to have a Regex that is executable in node.js (so no jQuery DOM Handling etc., because the tags can have a different nesting) that matches all the text that is NOT a HTML tag or part of it into seperate groups.

E.g. I'd like to match "5","ELT.","SPR"," ","plo","Unterricht"," ","&nbsp" and "plo" from that String:

<tr class='list even'>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <span style="color: #010101">5</span>
    </td>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <b><span style="color: #010101">ELT.</span></b>
    </td>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <b><span style="color: #010101">SPR</span></b>
    </td>
    <td class="list" style="background-color: #FFFFFF" >&nbsp;</td>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <strike><span style="color: #010101">pio</span></strike>
    </td>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <span style="color: #010101">Unterricht</span>
    </td>
    <td class="list" style="background-color: #FFFFFF" >&nbsp;</td>
    <td class="list" style="background-color: #FFFFFF" >&nbsp;</td>
    <td class="list" align="center" style="background-color: #FFFFFF" >
        <b><span style="color: #010101">pio</span></b>
    </td>
</tr>

I can assure that there will be no ">"'s within the tags.

The solution I found was (?<=^|>)[^><]+?(?=<|$), but that won't work in node.js (probably because the lookaheads? It says "Invalid group")

Any suggestions? (and yes, I really think that Regex is the right way to go because the html may be nested in other ways and the content always has the same order because it's a table)

like image 388
iStefo Avatar asked Sep 24 '11 17:09

iStefo


2 Answers

Try 'yourhtml'.replace(/(<[^>]*>)/g,' ')

'<tr class="list even"><td class="list" align="center" style="background-color: #FFFFFF" ><span style="color: #010101">5</span></td><td class="list" align="center" style="background-color: #FFFFFF" ><b><span style="color: #010101">ELT.</span></b></td><td class="list" align="center" style="background-color: #FFFFFF" ><b><span style="color: #010101">SPR</span></b></td><td class="list" style="background-color: #FFFFFF" > </td><td class="list" align="center" style="background-color: #FFFFFF" ><strike><span style="color: #010101">pio</span></strike></td><td class="list" align="center" style="background-color: #FFFFFF" ><span style="color: #010101">Unterricht</span></td><td class="list" style="background-color: #FFFFFF" > </td><td class="list" style="background-color: #FFFFFF" > </td><td class="list" align="center" style="background-color: #FFFFFF" ><b><span style="color: #010101">pio</span></b></td></tr>'.replace(/(<[^>]*>)/g,' ')

It will give a space delimited text that you want to match (which you can split on space).

like image 134
Narendra Yadala Avatar answered Sep 20 '22 08:09

Narendra Yadala


Maybe you can split directly using the tags themselves:

html.split(/<.*?>/)

Afterwards you have to remove the empty strings from the result.

like image 33
Howard Avatar answered Sep 21 '22 08:09

Howard