0

我不明白为什么我的情况不起作用

$('#resPage').live('pageshow', function(event) {

    application.previousButton();

    //get pdf
    var text = '';
    var value;
    var get = application.readGet();
    switch(get['id']){
        case '1':
            var patt = /annales/g;
            var cutLen = 20;
            break;
         case '2':
             var patt = /copies/g;
             var cutLen = 19;
             break;
    }
    $.ajax({
        url:application.api+'/ressources',
        success:function(data){
            text = "<ul data-role='listview' data-inset='true' >"
            data = JSON.parse(data);
            for(elem in data.files){
            console.log(elem)
            console.log(patt.test(data.files[elem]));
            if(patt.test(data.files[elem])){
                console.log('add');
                value = data.files[elem].substr(1);
                text += application.textRelLink(value,cutLen);
            }
        }   
        text +="</ul>" 
        $('#resPage .content').html(text);
        $('#resPage .content ul').listview();
        }
    })
});

在我的日志中,我有:

03-02 11:03:14.722: D/CordovaLog(23226): 0
03-02 11:03:14.722: D/CordovaLog(23226): false
03-02 11:03:14.722: D/CordovaLog(23226): 1
03-02 11:03:14.722: D/CordovaLog(23226): false
03-02 11:03:14.722: D/CordovaLog(23226): 2
03-02 11:03:14.722: D/CordovaLog(23226): true
03-02 11:03:14.722: D/CordovaLog(23226): 3
03-02 11:03:14.722: D/CordovaLog(23226): true
03-02 11:03:14.730: D/CordovaLog(23226): 4
03-02 11:03:14.730: D/CordovaLog(23226): true
03-02 11:03:14.730: D/CordovaLog(23226): 5
03-02 11:03:14.745: D/CordovaLog(23226): false
03-02 11:03:14.745: D/CordovaLog(23226): 6
03-02 11:03:14.745: D/CordovaLog(23226): false

但我从来没有在真实时添加

谢谢您的帮助

编辑:

解析前的数据值

03-02 11:25:05.253: D/CordovaLog(23611): {"files":[".\/thumbs\/pdf\/methodo\/sdsdfsd_sdf.pdf",".\/thumbs\/pdf\/methodo\/test!!.pdf",".\/thumbs\/pdf\/annales\/ddfgdfg.pdf",".\/thumbs\/pdf\/annales\/big.pdf",".\/thumbs\/pdf\/annales\/test.pdf",".\/thumbs\/pdf\/copies\/test.pdf",".\/thumbs\/pdf\/poly\/gdfhf.pdf"],"dirs":[".\/thumbs\/pdf\/poly\/",".\/thumbs\/pdf\/copies\/",".\/thumbs\/pdf\/annales\/",".\/thumbs\/pdf\/methodo\/"]}
4

1 回答 1

1

重新测试前需要重置lastIndex正则表达式的属性

console.log(patt.test(data.files[elem]));
patt.lastIndex = 0;
if(patt.test(data.files[elem])){

或者,如果您g从正则表达式中删除不必要的标志,则无需 reset lastIndex

当正则表达式与g标志一起使用时,它会跟踪找到最后一个匹配项的位置,以便下一个测试从该点开始搜索,即lastIndex.

请参阅为什么在 Javascript 中带有全局标志的 RegExp 会给出错误的结果?.

于 2013-03-02T11:02:01.670 回答