正则表达式选择表中的2个元素

时间:2016-04-29 04:13:57

标签: regex regex-lookarounds

如何在此表中仅使用正则表达式选择值“CLARO”?

我怎样才在同一张桌子上选择“AMBA”值?

<div style=" height:40px;"></div>                 
        <table border="0" cellpadding="0" cellspacing="0" align="center">
            <tr> 
                <td width="120" class="estilotd">   Línea </td>
                <td width="250" class="estilotd">   Operador </td>
                <td width="150" class="estilotd">   Localidad</td>
                <td width="150" class="estilotd">   Servicio</td>
            </tr>

            <tr> 
                <td class="estilotd2">1157670327 </td>
                <td class="estilotd2"> CLARO </td>
                <td class="estilotd2"> AMBA </td>
                <td class="estilotd2"> TELEFONIA MOVIL </td>
            </tr>
        </table> 
        <div style="width:665px; height:30px; margin:80px 0 0 70px;">
            <div style="float:left">
            <form action="detectar_empresa_celular.php" target="_self">
                <input type="submit" class="boton" value="â Realizar otra búsqueda">
            </form>
            </div>
            <div style="float:right">
            <form action="titular_de_numero_celular.php" target="_self">
                <input type="submit" class="boton" value="Solicitar Titular de Celular âº">
            </form>
            </div>
        </div>   

1 个答案:

答案 0 :(得分:0)

如果您知道这些值(即您总是想要找到CLARO和AMBA),那就很简单了:

在javascript中:

var claro =/<td class="estilotd2">( CLARO )(?=<\/td>)/;
var amba = /<td class="estilotd2">( AMBA )(?=<\/td>)/; 

如果你不是在javascript中它变得更容易,因为我们可以使用负面的lookbehinds:

(?<=<td class="estilotd2">)( CLARO )(?=<\/td>)

如果你想要更普遍地捕捉,你可以这样做:

的javascript:

var all = /<td class="estilotd2">([^&]*?)(?=<\/td>)/g;
var matches;
while (matches = all.exec(str)) {
   console.log(matches[1]);
}

fiddle

非JavaScript:

   (?<=<td class="estilotd2">)(.*?)(?=<\/td>)

最后......如果您在浏览器中工作,似乎可以更容易地使用选择器来查找该表中的值。例如:

var elements = document.getElementsByClassName('estilotd');
for(var i = 0; i < elements.length; i++){
    console.log(elements[i].innerText);
};

fiddle