我有這種類型的JSON數組:支架正則表達式的Javascript
[
{ text: '[Chapter1](chapter1.html)'},
{ text: '[Chapter2](chapter2.html)'},
{ text: '[Chapter3](chapter3.html)'},
{ text: '[Chapter4](chapter4.html)'}
]
在試圖環槽的陣列和取括號中的文本(第1章,第2章等)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);
應打印:
["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)' ]
我缺少什麼?我吮吸正則表達式。
不知道你用JSON.parse那裏所做的事情,但這裏有一個[** ** FIDDLE(HTTP://的jsfiddle。淨/ xGXD8/2 /),也許這使得它更清晰? – adeneo
我使用GET請求從外部服務器獲取JSON。我不知道什麼是錯的。我檢查了一切。它仍然返回甚至不在json數組中的字段。 – jviotti
@adeneo我附上了我得到的結果 – jviotti