我有这种类型的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