2016-11-11 9 views
1

私は非常にゆっくりと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米
米国630、

私は延々とforループで手を加えてきたと方法のこの種を試してみましたが、エラーを打つ保つ:

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

私はちょうどこれを割るように見えることはできません。私は何か根本的に間違っていることを知っている、私はちょうど何を知らない。

多くのありがとうございます。

+0

あなたがエラーを投稿することができます??:あなたはこのような何かをしようとすると何

youssouf

答えて

1

Miguel氏によると、JadeコードではJavaScriptよりも問題が多いと私は考えています。すべてのマッチを1つのliの中に入れようとしています:li #{matches}

ul 
each item in matches 
    li= item 
+1

それはトリックでした!あなたのおかげで多くの – DanielH

2

翡翠にはあまりよく慣れていませんが、{マッチ}を開梱していないようです。 https://pugjs.org/language/iteration.html [翡翠はPugに改名されたと言えるでしょう]

+0

"翡翠はPugに改名されましたか?"はい。 – undefined