Count Regex Matches

Count how many times a regex pattern matches in a string.

Code

General
(str.match(new RegExp(pattern, 'g')) || []).length;

Parameters

String to search

Pattern to count

Browser·fetch() may be limited by CORS

More JavaScript Snippets