2016-09-12 4 views
-1

私はこの場合があります:なぜこの正規表現はJavaScriptの実装で正しくグループに一致しませんか?

<a></a>要素のhrefリンクと一致する必要があります。 このためMY正規表現は、それが<a href="([^"]*)"

だ、ここで働くことを確認してください - >https://regex101.com/r/tB1hJ0/1

私はJavaScriptでこの正規表現を使用しようとすると問題があります。

私は私が同じ結果を得るいけないの実装使用してみた場合:私が間違っているのは何

var input = '2\ 
<p><a href="http://www.quackit.com/html/tutorial/html_links.cfm">Example Link</a></p>\ 
<div class="more-info"><a href="http://www.quackit.com/html/examples/html_links_examples.cfm">More Link Examples...</a></div>'; 

console.log((/<a href="([^"]*)"/gmi).exec(input)); //["<a href="http://www.quackit.com/html/tutorial/html_links.cfm"", "http://www.quackit.com/html/tutorial/html_links.cfm", 
console.log(input.match(/<a href="([^"]*)"/gmi)); //["<a href="http://www.quackit.com/html/tutorial/html_links.cfm"", "<a href="http://www.quackit.com/html/examples/html_links_examples.cfm""] 

を?

+1

私は正確にキャプチャするwrong..what何も表示されません? –

答えて

0

は、あなたがキャプチャグループではなく、一致するテキストを取る必要があり、全体の正規表現マッチ

/<a href="([^"]*)"/gm

+0

Alexis答えは基本的にちょうど私の後で送られます。 – snit80

1

のためにこれを必要とします。

var re = /<a href="([^"]*)"/gm; 
 
var str = '2\n<p><a href="http://www.quackit.com/html/tutorial/html_links.cfm">Example Link</a></p>\n<div class="more-info"><a href="http://www.quackit.com/html/examples/html_links_examples.cfm">More Link Examples...</a></div>'; 
 
match = re.exec(str); 
 
while (match != null) { 
 
    console.log("Matching text:"+match[0]) 
 
    console.log("Matching group:"+match[1]);//You need this 
 
    match = re.exec(str); 
 
}

+0

ああ、これはうまくいくが、私はこの振る舞いをうんざらしていない。別の時に私はこの 'console.log(" 1m .... .22wwdhdhd 44dadaa 2 ".match(/(\ d +)[az] +/gmi));'のような複数のマッチを扱い、良い。 –

関連する問題