I have a JSON array of this type:
[
{ text: '[Chapter1](chapter1.html)'},
{ text: '[Chapter2](chapter2.html)'},
{ text: '[Chapter3](chapter3.html)'},
{ text: '[Chapter4](chapter4.html)'}
]
In an attempt to loop trough the array and fetch the text in brackets (Chapter1, Chapter2, etc) I found a RegExp here at StackOverflow.
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);
That should print:
["Chapter1","Chapter2","Chapter3","Chapter4"]
However I get weird long results in multiple arrays:
[ '[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)' ]
What am I missing? I suck at regexps.