需求:
我們有一個這樣的字符串
`以下數(shù)據(jù):{"title": "標題一", "text": "內(nèi)容一", "tag": "tag1"}{"title": "標題二", "text": "內(nèi)容二", "tag": "tag二"}`
需要提取里面的字符串文章來源:http://www.zghlxwxcb.cn/news/detail-683171.html
function extractDataFromString(str) {
const regexTitle = /"title": "(.*?)"/g;
const regexText = /"text": "(.*?)"/g;
const regexTag = /"tag": "(.*?)"/g;
let titles = [];
let texts = [];
let tags = [];
let match;
while ((match = regexTitle.exec(str))) {
titles.push(match[1]);
}
while ((match = regexText.exec(str))) {
texts.push(match[1]);
}
while ((match = regexTag.exec(str))) {
tags.push(match[1]);
}
let result = [];
for (let i = 0; i < titles.length; i++) {
let obj = {
title: titles[i],
text: texts[i] || "",
tag: tags[i] || ""
};
result.push(obj);
}
return JSON.stringify(result);
}
const jsonData = extractDataFromString(inputString);
console.log(jsonData);
golang版本文章來源地址http://www.zghlxwxcb.cn/news/detail-683171.html
到了這里,關(guān)于使用正則提取字符串中的json數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!