在文件中查找缺少的序列号 [英] Finding the missing sequence number in a file
本文介绍了在文件中查找缺少的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我必须找到一个XML文件中丢失的数字。但我觉得很难找到。请提出一些想法。
示例
文件包含< a>
标签,其中包括ID即页面1,2 ...我需要找到使用jquery丢失的数字。
$ b $ a $
< p>有一个很棒的< a id =page-1/>你好吗?< / p>
< p>< a id =page-2/>祝您有美好的一天。< / p>
< p>您< a id =page-5/>是< / p>
< p>人生如此精彩< a id =page-6/>< / p>
我的代码
< script>
$ p
$(document).ready(function(){
$(#find)。click(function(){
Fname = $(#myFile) .val();
lpage = $(#lpage).val();
$ .get(Fname,function(data){
var lines = data .blit(\\\
);
if(lines.match(id =page-)){//如何使用常规exoreessi0n
alert(hi);
}
})
$ lt; / script>
$ b解决方案我将对您的输入做一些假设,请根据需要添加验证:
var lines = ['< p>有很棒的< a id =page-1/>天。 ',
'< p>< a id =page-2/>祝您有个愉快的一天。 =page-5/>是你< / p>' ,
'< p>人生如此精彩< a id =page-6/>< / p>];
var sequences = [];
lines.forEach(function(line){
//效率不高,但是简单
//让jQuery把这个字符串解析成对象,所以我们不需要regexp
var obj = $(line);
//假设总是有A和ID属性
var id = obj.find(a)。attr(id)。 split( - );
//假设序列总是最后
sequences.push(parseInt(id [id.length - 1]));
});
sequences.sort();
//再一次,效率低下,但会做这个工作
var last = sequences [sequences.length - 1];
$ b $ for(var i = last; i> = 0; i--){
if(sequences.indexOf(i)< 0){
console.log (我+失踪);
}
}
Hi I have to find a missing number in an xml file. but I feel difficulty to find. Pls suggest some ideas.
Example
A file contains an
<a>
tag which include id i.e page-1,2... I need to find the missing numbers using 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>
My code
<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>
解决方案I'll take some assumptions regarding your input. Please add validations as necessary:
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"); } }
这篇关于在文件中查找缺少的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文