admin管理员组

文章数量:1296910

This is what regex should find { anything in it }, and then I want to count the number of results what the regex found.

So I have a string like this:

{example1}{example2}{example3} in this case the count number is 3

This is what regex should find { anything in it }, and then I want to count the number of results what the regex found.

So I have a string like this:

{example1}{example2}{example3} in this case the count number is 3

Share Improve this question asked Aug 5, 2010 at 22:49 Adam HalaszAdam Halasz 58.3k67 gold badges153 silver badges216 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

you need the global regex flag (g) to match all occurrences and then simply get the length of the result. the ? makes the .* ungreedy, otherwise it would only once fit the first and the last bracket as regexps are greedy by default.

var source = "{example1}{example2}{example3}";
var count = source.match(/\{.*?\}/g).length;

本文标签: JAVASCRIPT Count the number of REGEX resultsStack Overflow