Home >Web Front-end >JS Tutorial >Use regular expressions in JavaScript to match multiple items and obtain the grouped data in each item_javascript skills

Use regular expressions in JavaScript to match multiple items and obtain the grouped data in each item_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:14:521336browse

However, what I have to face is a very loose HTML page that cannot be XML
Therefore, the focus of this article is that if the web page to be obtained contains Table or List paragraphs, the information of these paragraphs needs to be saved to JS in the form of columns. Paste the code directly into the array
:

Copy the code The code is as follows:

var str = "String string< tr>< ;td>李思< td>685654
IDNamePhone
01Zhang San1234567
02343434
03Wang Wu
String string";
var regRecord = new RegExp('\\([0-9]{ 2})\<\/td\>\([^\<] )\<\/td\>\([0-9] )\<\ /td\>\<\/tr\>','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]
});
}
// At this time, g_records saves the matching data table, print it out and see:
for(var i=0;ialert("ID:" g_records[i].Id ";Name:" g_records[i].Name ";Phone:" g_records[i].Phone);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn