1

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.

Community
  • 1
  • 1
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • Not sure what you're doing there with the JSON.parse, but here's a [**FIDDLE**](http://jsfiddle.net/xGXD8/2/), maybe that makes it clearer ??? – adeneo Dec 14 '12 at 04:36
  • I'm getting the JSON from an external server with a GET request. I don't know whats wrong. I've inspected everything. It still returns field that are not even in the json array. – jviotti Dec 14 '12 at 04:41
  • @adeneo I attached the result I'm getting – jviotti Dec 14 '12 at 04:46
  • Oh, you're logging the result of the `exec` which is not all match, but all match + information. Checkout the fiddle @adeneo posted, at the end of his code, he filters out the info you need. – Simon Boudrias Dec 14 '12 at 04:50

3 Answers3

1

The exec method of regular expressions returns not just the matched text but a lot of other information, including the input, match index, matched text, and text of all captured groups. You probably want match group 1:

result.push(sectionRegex.exec(aResponse[x].text)[1]);

Besides that, you shouldn't use for(...in...) loops to loop over arrays because that will break if any methods are added to Array's prototype. (e.g., forEach shims)

icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

Not as weird as you think, each regex.exec result is effectively an object that looks like one of those blocks, it contains the whole text matched, the subgroups matched (you have only one subgroup, and it's the result you actually want), the index within the input that the match succeeded and the input that was given.

All of this is valid and useful results from a successful match.

Short answer, is you want to push only the 2nd array element into the results.
Like regex.exec(text)[1].

dlamblin
  • 43,965
  • 20
  • 101
  • 140
0

The regular expression you use will return an array. The first element will be the string to test. The next element will be the matche between parenthesis Try this:

result.push(sectionRegex.exec(aResponse[x].text)[1]); 
Jamal
  • 69
  • 1
  • 4