Javascript 条件和正则表达式phonegap

Javascript 条件和正则表达式phonegap,javascript,regex,cordova,Javascript,Regex,Cordova,我不明白为什么我的情况不起作用 $('#resPage').live('pageshow', function(event) { application.previousButton(); //get pdf var text = ''; var value; var get = application.readGet(); switch(get['id']){ case '1': var patt =

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

$('#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: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\/"]}

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

console.log(patt.test(data.files[elem]));
patt.lastIndex = 0;
if(patt.test(data.files[elem])){
或者,如果从正则表达式中去掉不必要的
g
标志,则无需重置
lastIndex

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


请参阅。

我可以在ajax的成功块中查看
数据的返回值吗?我在分析之前添加了数据。我认为您在这里的条件
patt.test(data.files[elem])
不是真的。这就是为什么您从未在日志中打印
add
。好的,但我的日志中有true,这是相同的条件
console.log(patt.test(data.files[elem]));
patt.lastIndex = 0;
if(patt.test(data.files[elem])){