2013-09-23 2 views
1

JS에서 RegExp와 일치하는 항목 수를 얻을 수 있습니까? 그 수를 얻을 수있는 방법 그래서JS RegExp 일치 항목 수

var pattern =/\bstring\b/g; 
var str = "My the best string comes here. Do you want another one? That will be a string too!"; 

: 의이 simpliest 예를 가정 해 보자? 나는 표준 방법 간부()를 사용하려고하면 :

pattern.exec(str); 

... 그것은 나에게 배열을 보여줍니다, 독특한 일치하는 항목이 포함이 배열의 아이폰에 1이

["string"] 

만에 현실 일치하는 항목이 발견 된 2 점이 있습니다.

답변

1

당신은 .match()를 사용하여 얻을 수 있습니다

var pattern =/\bstring\b/g; 
var str = "My the best string comes here. Do you want another one? That will be a string too!"; 
alert(str.match(pattern).length); 

데모 Fiddle