私は非常にゆっくりとJavascriptを学んでおり、for
ループを正規表現マッチで実装するのが難しいです。Javascriptのループオーバー正規表現の場合
私は私の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 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 < matches.length; i += 1) {
router.get('/', function(req, res) {
res.render('index', { matches, matchesTwo, i }) });
};
module.exports = router;
に、私はHTMLのためのジェイドを使用していて、私のindex.jade
ファイルはこのようになります以下で、express.js
セットアップで働いている:
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}
私はそれが一致したいものと一致する限り、コードは動作します。私はテキストのあなたの次の米国の例を見つけ
出力
:私がいる問題は、HTML出力は次のようになりますので、試合のすべてが、同じ行にバンドルを取得しているということです。を私に提供:。。
- Grutter Vボリンジャー、539米国306、326(2003)、ショウVリノ、509米国630、650(1993)、クロフォードVワシントン、123米国123、123(2016 )
私が探している出力は、次のとおりです。
所望の出力
は、私はあなたが私を提供したテキストに以下の米国のケースを見つけました:
- Grutter Vボリンジャー、539米国306、 326(2003)
- ショウV。リノ、509(1993)
- クロフォード対ワシントン、123、123(2016)123米
私は延々とfor
ループで手を加えてきたと方法のこの種を試してみましたが、エラーを打つ保つ:
for (var i = 0; i < matches.length; i += 1) {
document.body.innerHTML += matches[i] + '<br>';
}
私はちょうどこれを割るように見えることはできません。私は何か根本的に間違っていることを知っている、私はちょうど何を知らない。
多くのありがとうございます。
あなたがエラーを投稿することができます??:あなたはこのような何かをしようとすると何
– youssouf