How to extract a string using JavaScript Regex?

后端 未结 5 866
梦毁少年i
梦毁少年i 2020-11-30 04:26

I\'m trying to extract a substring from a file with JavaScript Regex. Here is a slice from the file :

DATE:20091201T220000
SUMMARY:Dad\'s birthday

5条回答
  •  天命终不由人
    2020-11-30 04:48

    this is how you can parse iCal files with javascript

        function calParse(str) {
    
            function parse() {
                var obj = {};
                while(str.length) {
                    var p = str.shift().split(":");
                    var k = p.shift(), p = p.join();
                    switch(k) {
                        case "BEGIN":
                            obj[p] = parse();
                            break;
                        case "END":
                            return obj;
                        default:
                            obj[k] = p;
                    }
                }
                return obj;
            }
            str = str.replace(/\n /g, " ").split("\n");
            return parse().VCALENDAR;
        }
    
        example = 
        'BEGIN:VCALENDAR\n'+
        'VERSION:2.0\n'+
        'PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n'+
        'BEGIN:VEVENT\n'+
        'DTSTART:19970714T170000Z\n'+
        'DTEND:19970715T035959Z\n'+
        'SUMMARY:Bastille Day Party\n'+
        'END:VEVENT\n'+
        'END:VCALENDAR\n'
    
    
        cal = calParse(example);
        alert(cal.VEVENT.SUMMARY);
    

提交回复
热议问题