var testString = "There are 3 cats but 4 dogs.";
var expression = /\d+/g;
var digitCount = testString.match(expression).length;
// one kind of special digital selector is the selector \d
means is used to obtain a numeric string.
// In JavaScript, digital selector similar to: /\d/g
.
// add a plus sign after the selector ( +
), for example: /\d+/g
it allows the regular expression matching one or more numbers.
// tail g
is 'global' shorthand, which means allow this regex find all matches rather than just the first match.
Reproduced in: https: //www.cnblogs.com/chendi618/p/11000472.html