2017-08-01 14 views
-2

は考える:各文字列を一致させるには?

var countries = ["Afghanistan", "United Kingdom", "Albania", "United Arab Emirates"]; 
var regex = new RegExp(countries.join("|"), "i"); 

$wikiDOM.find(".infobox td div li a").each(function(){ 
    var newtext = $(this).text(); 
    //console.log(newtext); 
    console.log(newtext.match(regex)); 
}); 

私がしなければ:

console.log(newtext); 

私が手:

White 
Asian 
Black 
Mixed 
Arab 
1] 
United Kingdom 
England and Wales 
^ 
Legislative Grand Committee 

だから、私はconsole.log(newtext.match(regex));を実行すると、コンソールが私を与える、まだUnited Kingdomに一致するものを持っていますnull null null...

私は、配列内の文字列に対して文字列の一致を探しています。一致するものがあれば、それをコンソールに出力します。この場合、それは我々が配列で持っている私は.each()経由で取得したリストが実際に私を与えてイギリスたjsFiddle

+0

あなたはどんな出力を探していますか? – mkaatman

+0

@mkaatman私はそれが正しい一致であるように私に '英国 'を与える​​ためのコンソールを探しています –

+0

私はあなたがここで何を求めているのか分かりません? – Liam

答えて

0

で、まだHTMLはUnited Kingdomは、したがって、私はでした。ここUnited Kingdom

だろう:

$wikiDOM.find(".infobox td div li a").each(function(){ 
    var newtext = $(this).html(); 
    var newtextStriped = newtext.replace(/ /g,' '); 
    if($.inArray(newtextStriped, countries) != -1){ 
    console.log(newtextStriped); 
    } 
}); 
関連する問題