在Javascript中循环使用正则expression式匹配

我很慢学习Javascript,并且难以在正则expression式匹配上实现for循环。

我正在express.js设置工作,在我的index.js有以下内容

 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 US 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 US 630, 650 (1993). Daniel added the following test case to the string: Crawford v. Washington, 123 US 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 < matches.length; i += 1) { router.get('/', function(req, res) { res.render('index', { matches, matchesTwo, i }) }); }; module.exports = router; 

我使用Jade的HTML和我的index.jade文件如下所示:

 extends layout block content body h1 CaseSpy p I found the following US 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} 

代码工作,只要它符合我想要它匹配。 我遇到的问题是所有的匹配都捆绑在同一行上,所以html输出如下所示:

产量

我在你提供给我的文本中find以下美国案例:

  • Grutter诉Bollinger,539 US 306,326(2003),Shaw诉Reno,509 US 630,650(1993),Crawford诉华盛顿,123 US 123,123(2016)

我正在寻找的输出是:

期望的输出

我在你提供给我的文本中find以下美国案例:

  • Grutter诉Bollinger,539 US 306,326(2003)
  • Shaw诉Reno,509 US 630,650(1993)
  • 克劳福德诉华盛顿,123 US 123,123(2016)

我已经无休止地修改了for循环,并尝试了这种方法,但仍然遇到错误:

 for (var i = 0; i < matches.length; i += 1) { document.body.innerHTML += matches[i] + '<br>'; } 

我似乎无法破解这一点。 我知道我在做一些根本性的错误,我只是不知道该怎么做。

非常感谢。

正如Miguel所说的,我相信这个问题可能比使用JavaScript的Jade代码更多。 你正在尝试把所有的匹配放在一个单一的里面,就像这样: li #{matches}

如果你尝试这样的事情呢?

 ul each item in matches li= item 

对Jade不太熟悉,但看起来你并没有将{matches}解包。 这可能有所帮助: https : //pugjs.org/language/iteration.html [从我能告诉翡翠改名帕格?]