このタイプの JSON 配列があります。
[
{ text: '[Chapter1](chapter1.html)'},
{ text: '[Chapter2](chapter2.html)'},
{ text: '[Chapter3](chapter3.html)'},
{ text: '[Chapter4](chapter4.html)'}
]
配列をループして括弧内のテキスト (Chapter1、Chapter2 など) を取得しようとして、StackOverflow で RegExp を見つけました。
var aResponse = JSON.parse(body).desc; // the array described above
var result = [];
var sectionRegex = /\[(.*?)\]/;
for(var x in aResponse) {
result.push(sectionRegex.exec(aResponse[x].text));
//console.log(aResponse[x].text) correctly returns the text value
}
console.log(result);
それは印刷する必要があります:
["Chapter1","Chapter2","Chapter3","Chapter4"]
ただし、複数の配列で奇妙な長い結果が得られます。
[ '[Chapter1]',
'Chapter1',
index: 0,
input: '[Chapter1](chapter1.html)' ]
[ '[Chapter2]',
'Chapter2',
index: 0,
input: '[Chapter2](chapter2.html)' ]
[ '[Chapter3]',
'Chapter3',
index: 0,
input: '[Chapter3](chapter3.html)' ]
[ '[Chapter4]',
'Chapter4',
index: 0,
input: '[Chapter4](chapter4.html)' ]
私は何が欠けていますか?私は正規表現が苦手です。