Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:43:17+00:00 2026-06-15T19:43:17+00:00

I have a JSON array of this type: [ { text: ‘[Chapter1](chapter1.html)’}, { text:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:43:18+00:00Added an answer on June 15, 2026 at 7:43 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code on the server side: <?php header('Content-Type: text/html; charset=utf-8'); require ../general.variables.php;
I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I have a json object that looks something like this: Array ( [algorithm] =>
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
I have some troubles mapping a JSON Array to RestKit. This is what the
I have to loop in a JSON array to get some informations in node
i have this json array response: [ { status: active, url: https:\/\/ikiosk.podio.com\/dev\/apps\/myusers, url_label: myusers,
I have this code: <?php header('Content-Type: text/javascript; charset=UTF-8'); header('Cache-Control: private, no-cache, no-store, must-revalidate'); header('Pragma:
hi have this code which works perfectly with just value: <script type=text/javascript> $(document).ready(function() {
I have a jsonArray something like this. var json_array = [ { text: id,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.