title: For loop over regex matches in Javascript
body: <p>I'm very slowly learning Javascript and am having difficulty implementing a <code>for</code> loop over regex matches. </p>
  
  <p>I'm working in an <code>express.js</code> setup, with the following in my <code>index.js</code></p>
  
  <pre><code>var express = require('express');
  
  var router = express.Router();
  
  // the text block against which I am running the regex
  
  var para = "We have insisted on strict scrutiny in every context, even for so-called “benign” racial classifications, such as race-conscious university admissions policies, see Grutter v. Bollinger, 539 U.S. 306, 326 (2003), race-based preferences in government contracts, see Adarand, supra, at 226, and race-based districting intended to improve minority representation, see Shaw v. Reno, 509 U.S. 630, 650 (1993). Daniel added the following test case to the string: Crawford v. Washington, 123 U.S. 123, 123 (2016)."
  
  // regex
  
  var regex = /(\w+\sv.\s\w+,\s\d*\s?[\w.]*[\d,\s]*\(\d{4}\))/ig;
  var regexTwo = /\w+\s/ig;
  
  // Store the matches
  
  var matches = para.match(regex);
  var matchesTwo = para.match(regexTwo);
  
  // the offending loop
  
  for (var i = 0; i &lt; matches.length; i += 1) {
  
      router.get('/', function(req, res) {
  
          res.render('index', { matches, matchesTwo, i }) });
  
  };
  
  module.exports = router;
  </code></pre>
  
  <p>I'm using Jade for the HTML and my <code>index.jade</code> file looks like this:</p>
  
  <pre><code>extends layout
  
  block content
  
      body
          h1 CaseSpy
          p I found the following U.S. cases in the text you provided me with:
          ul
              li #{matches}
  
          p I found the following other terms in the text you provided me with:
          ul
              li #{matchesTwo}
  </code></pre>
  
  <p>The code works, insofar as it matches what I want it to match against. The problem I'm having is that all of the matches are getting bundles up on the same line, so the html output looks like this:</p>
  
  <p><strong>Output</strong></p>
  
  <p>I found the following U.S. cases in the text you provided me with:</p>
  
  <ul>
  <li>Grutter v. Bollinger, 539 U.S. 306, 326 (2003),Shaw v. Reno, 509 U.S. 630, 650 (1993),Crawford v. Washington, 123 U.S. 123, 123 (2016)</li>
  </ul>
  
  <p>The output I'm looking for is:</p>
  
  <p><strong>Desired Output</strong></p>
  
  <p>I found the following U.S. cases in the text you provided me with:</p>
  
  <ul>
  <li>Grutter v. Bollinger, 539 U.S. 306, 326 (2003)</li>
  <li>Shaw v. Reno, 509 U.S. 630, 650 (1993)</li>
  <li>Crawford v. Washington, 123 U.S. 123, 123 (2016)</li>
  </ul>
  
  <p>I've tinkered with the <code>for</code> loop endlessly and have tried this sort of method but keep hitting errors:</p>
  
  <pre><code>for (var i = 0; i &lt; matches.length; i += 1) {
    document.body.innerHTML += matches[i] + '&lt;br&gt;';
  }
  </code></pre>
  
  <p>I just can't seem to crack this. I know I'm doing something fundamentally wrong, I just don't know what. </p>
  
  <p>Many thanks. </p>