查找文件中缺少的序列号

时间:2016-10-15 09:30:53

标签: javascript jquery html find sequence

您好我必须在xml文件中找到丢失的号码。但我觉得很难找到。请提出一些想法。

实施例

一个文件包含一个<a>标签,其中包含id,即第1,2页......我需要使用jquery找到丢失的数字。

A.XML

<p>have a great <a id="page-1"/>day. How are you.</p>
<p><a id="page-2"/>Have a nice day.</p>
<p>How <a id="page-5"/>are you</p>
<p>Life is so exciting<a id="page-6"/></p>

我的代码

<script>
    $(document).ready(function() {      
        $("#find").click(function(){
            Fname = $("#myFile").val();
            lpage = $("#lpage").val();

            $.get(Fname, function(data){
                var lines = data.split("\n");
                if (lines.match(id="page-)) { //how to use regular exoreessi0n
                    alert("hi");
                }
            });
        });
    });
</script>   

1 个答案:

答案 0 :(得分:0)

我会对你的输入做一些假设。请根据需要添加验证:

var lines = ['<p>have a great <a id="page-1"/>day. How are you.</p>',
    '<p><a id="page-2"/>Have a nice day.</p>',
    '<p>How <a id="page-5"/>are you</p>',
    '<p>Life is so exciting<a id="page-6"/></p>'];

    var sequences = [];
    lines.forEach(function(line) {

        // Not efficient but simple
        // Let jQuery parse this string to object, so we won't need regexp
        var obj = $(line);

        // Assuming there's always A and ID attribute
        var id = obj.find("a").attr("id").split("-");

        // Assuming sequence is always last
        sequences.push(parseInt(id[id.length - 1]));
    });

    sequences.sort();

    // Again, inefficient, but will do the job
    var last = sequences[sequences.length - 1];

    for (var i = last; i >= 0; i--) {
        if (sequences.indexOf(i) < 0) {
            console.log(i + " is missing");
        }
    }