var str = "字符串字符串
ID |
姓名 |
电话 |
01 |
张三 |
1234567 |
02 |
李四 |
343434 |
03 |
王五 |
685654 |
字符串字符串";
var regRecord = new RegExp('\\
\\([0-9]{2})\\\\ |
([^\\\\ |
([0-9]+)\\\\','g'); var fieldIndex = { 'Id' : 1 , 'Name' : 2 , 'Phone' : 3 } var g_records = [],record; while ((record = regRecord.exec(str)) != null){ g_records.push({ 'Id' : RegExp["$"+fieldIndex.Id] ,'Name' : RegExp["$"+fieldIndex.Name] ,'Phone' : RegExp["$"+fieldIndex.Phone] }); } //此时 g_records 就保存了匹配的数据表格,打印出来看看: for(var i=0;ialert("ID:" + g_records[i].Id + ";Name:" + g_records[i].Name + ";Phone:" + g_records[i].Phone); }
|