解析HTML响应正文字符串以提取部件

时间:2014-02-12 19:24:54

标签: javascript jquery regex substring

我正在通过Ajax get请求收到一个响应正文,在该正文中我对某个特定部分感兴趣,如下所示:

if (w2.isHidden() == false) {
    var counterValue = 0;
    try {
        var url = site + '/user.widget?page=just_data&query=Select IFNULL( time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_skill';
        var loader = dhtmlxAjax.getSync(url);
        var text = loader.xmlDoc.responseText;
        var dataCells = text.split("<cell>");
        if (dataCells.length >= 2) {
            counterValue = dataCells[1];
            counterValue = counterValue.substring(0, counterValue.indexOf("<"));
        }
    } catch (err) {}
    var elem = document.getElementById("counter2_Value");
    elem.innerHTML = counterValue;
}

if (w5.isHidden() == false) {
    var counterValue = 0;
    try {
        var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_skill';
        var loader = dhtmlxAjax.getSync(url);
        var text = loader.xmlDoc.responseText;
        var dataCells = text.split("<cell>");
        if (dataCells.length >= 2) {
            counterValue = dataCells[1];
            counterValue = counterValue.substring(0, counterValue.indexOf("<"));
        }
    } catch (err) {}
    var elem = document.getElementById("counter5_Value");
    elem.innerHTML = counterValue;
}

if (w10.isHidden() == false) {
    var counterValue = 0;
    try {
        var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_vdn';
        var loader = dhtmlxAjax.getSync(url);
        var text = loader.xmlDoc.responseText;
        var dataCells = text.split("<cell>");
        if (dataCells.length >= 2) {
            counterValue = dataCells[1];
            counterValue = counterValue.substring(0, counterValue.indexOf("<"));
        }
    } catch (err) {}
    var elem = document.getElementById("counter10_Value");
    elem.innerHTML = counterValue;
}

if (w11.isHidden() == false) {
    var counterValue = 0;
    try {
        var url = site + '/user.widget?page=just_data&query=Select IFNULL( time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_vdn';
        var loader = dhtmlxAjax.getSync(url);
        var text = loader.xmlDoc.responseText;
        var dataCells = text.split("<cell>");
        if (dataCells.length >= 2) {
            counterValue = dataCells[1];
            counterValue = counterValue.substring(0, counterValue.indexOf("<"));
        }
    } catch (err) {}
    var elem = document.getElementById("counter11_Value");
    elem.innerHTML = counterValue;
}

if (w1.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(1, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name ", groupNumber as "Group Number", callsWaiting as " Calls Waiting ", longestWaiting as "Longest Waiting Call ", acceptableServiceLevel as "Acceptable Service Level", percentInServiceLevel as " Percent Within Service Level", staffed as "Staffed Agents ", available as " Agents Available" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid1.parse(loader.xmlDoc.responseText, 'xml');
            grid1.loadSortingFromCookie('grid1');
            grid1.loadSizeFromCookie('grid1');
        }
    } catch (err) {}
}

if (w6.isHidden() == false) {
    try {
        var url = site + '/user.widget?page=just_chart_data&query=SELECT SUM(available) as "Staffed Agents", SUM(staffed - available) as "Unavailable Agents" from  AvayaCMSAT_skill&widgetType=pie_medium';
        var loader = dhtmlxAjax.getSync(url);
        pie_medium6.clearAll();
        pie_medium6.parse(loader.xmlDoc.responseText, 'xml');
        pie_medium6.refresh();
    } catch (err) {}
}

if (w7.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(7, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", agentExt as "Agent Extension", groupName as "Group Name", groupNumber as "Group Number", state as "Agent State", IFNULL( time((statusDuration), "unixepoch") , time(0, "unixepoch")) as"Status Duration"  From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid7.parse(loader.xmlDoc.responseText, 'xml');
            grid7.loadSortingFromCookie('grid7');
            grid7.loadSizeFromCookie('grid7');
        }
    }
    catch (err) {}
}

if (w8.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(8, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", SUM(acdCalls) as " Acd Calls ", SUM(extInCalls) as " Extension In Calls ", SUM(extOutCalls) as " Extension Out Calls"  From AvayaCMSAT_agent GROUP BY groupName, groupNumber&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid8.parse(loader.xmlDoc.responseText, 'xml');
            grid8.loadSortingFromCookie('grid8');
            grid8.loadSizeFromCookie('grid8');
        }
    }
    catch (err) {}
}

if (w9.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(9, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", groupName as "Group Name", groupNumber as "Group Number", totalAcdCalls as "Total ACD Calls", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", totalAvailTime as "Total Available Time", totalAuxOthers as"Total Aux Others", extnCalls as "Extension Calls", avgExtnTime as "Average Extension Time", totalTimedStaff as "Total Time Staffed", totalHoldTime as "Total Hold Time"  From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid9.parse(loader.xmlDoc.responseText, 'xml');
            grid9.loadSortingFromCookie('grid9');
            grid9.loadSizeFromCookie('grid9');
        }
    }
    catch (err) {}
}

if (w3.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(3, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", percentInServiceLevel as"Percent Within Service Level", totalAcdCalls as "Total Acd Calls", avgSpeedAnswer as "Average Speed Answer", abandonCalls as "Abandoned Calls", avgAbandonTalkTime as "Average Abandon Talk Time", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", flowIn as "Flow In", flowOut as "Flow Out", totalAuxOther as " Total Aux/Other", avgStaff as" Average Staff" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid3.parse(loader.xmlDoc.responseText, 'xml');
            grid3.loadSortingFromCookie('grid3');
            grid3.loadSizeFromCookie('grid3');
        }
    }
    catch (err) {}
}

if (w4.isHidden() == false) {
    try {
        var url = site + '/user.widget?page=just_data&query=Select 0 AS"Min", ROUND((((SUM(totalAcdCalls * percentInServiceLevel)/100)*1.0)/SUM(totalAcdCalls))*100,2) AS"Cur",100 AS "Max" From AvayaCMSAT_skill';
        var loader = dhtmlxAjax.getSync(url);
        var text = loader.xmlDoc.responseText;
        var dataCells = text.split("<cell>");
        if (dataCells.length >= 3) {
            var speedoValue = dataCells[2];
            speedoValue = speedoValue.substring(0, speedoValue.indexOf("<"));
            $('#speedometer4_div').speedometer({
                percentage: speedoValue || 0
            });
        }
    }
    catch (err) {}
}

if (w12.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(12, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsWaiting as "Calls Waiting", longestWaitingCall as "Longest Waiting Call", acdCalls as "Acd Calls", avgSpeedTalk as"Average Speed Talk", abandonedCalls as "Abandoned Calls", avgAbandonTime as "Average Abandon Time", avgTalkHold as "Average Talk Hold", connCalls as "Conference Calls", flowOut as "Flow Out", callBusyDisc as "Call Busy Disconnect", percentInServLevel as "Percent In Service Level"  From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid12.parse(loader.xmlDoc.responseText, 'xml');
            grid12.loadSortingFromCookie('grid12');
            grid12.loadSizeFromCookie('grid12');
        }
    }
    catch (err) {}
}

if (w13.isHidden() == false) {
    try {
        var windowDims = getWindowStatus(13, 'grid');
        var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsOffered  as "Calls Offered", totalCalls as "Total Acd Calls", avgSpeedAnswered  as "Avg Speed Answered", totalAbandonedCalls as"Aboanded Calls", dayAvgAbandonTalk  as "AVG Abandon Talk Time", dayAvgTalkHold as "AVG Talk Hold", totalConnCalls as "Conn Calls", totalFlowOut as "Flow Out", totalCallBusyDisc as "Call Busy Disc", prctInServLevel as "Percent In Service Level"   From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
        var loader = dhtmlxAjax.getSync(url);
        if (loader.xmlDoc.responseText.length > 0) {
            grid13.parse(loader.xmlDoc.responseText, 'xml');
            grid13.loadSortingFromCookie('grid13');
            grid13.loadSizeFromCookie('grid13');
        }
    }
    catch (err) {}
}

如果你注意到上面的字符串中有一个重复的模式总是从以下开始:

if (w[number].isHidden() == false){

我对之后以及最后一个结束前的内容感兴趣。例如,在这种情况下:

if (w2.isHidden() == false) {

 //PART TO EXTRACT
      var counterValue = 0;
      try {
          var url = site + '/user.widget?page=just_data&query=Select IFNULL(time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch"))From vayaCMSAT_skill';
          var loader = dhtmlxAjax.getSync(url);
          var text = loader.xmlDoc.responseText;
          var dataCells = text.split("<cell>");
          if (dataCells.length >= 2) {
              counterValue = dataCells[1];
              counterValue = counterValue.substring(0, counterValue.indexOf("<"));
          }
      } 
      catch (err) {}
      var elem = document.getElementById("counter2_Value");
      elem.innerHTML = counterValue;
     //END PART TO EXTRACT
 }

如上所述,我有兴趣在开始和结束之间提取字符串并将其放入数组中。我希望能够为orignal响应正文字符串中显示的所有if语句执行此操作。

我已经看过使用regular expressions和indexOf()/ substring(),例如:

var start = html.indexOf("false) {", 0) + 8;
var end = html.indexOf("if (w", start);
alert(html.substring(start, end));

但是我没有设法在开括号和大括号之间得到字符串。

1 个答案:

答案 0 :(得分:1)

var tempWidgetContent = html.match(/w\d+\.isHidden(.*)\(\) == false\)[\s\S]*?catch\(err\)\{ \}/gm);

for (var i = 0; i < tempWidgetContent.length; i++) {
    var widgetContent = tempWidgetContent[i].substring(tempWidgetContent[i].indexOf('{') + 1);
}

这就是诀窍。