4

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

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - I misread this as a generic example, he's actually looking for the { and } :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.