Bulletproof getCountOfAllMatches against non-global regex input
FREEBIE
This commit is contained in:
parent
423a0fef67
commit
0496518af4
2 changed files with 16 additions and 0 deletions
|
@ -21,6 +21,10 @@
|
||||||
var match = regex.exec(str);
|
var match = regex.exec(str);
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
|
if (!regex.global) {
|
||||||
|
return match ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (match) {
|
while (match) {
|
||||||
count += 1;
|
count += 1;
|
||||||
match = regex.exec(str);
|
match = regex.exec(str);
|
||||||
|
|
|
@ -20,6 +20,18 @@ describe('EmojiUtil', function() {
|
||||||
var actual = emoji.getCountOfAllMatches(str, r);
|
var actual = emoji.getCountOfAllMatches(str, r);
|
||||||
assert.equal(actual, 2);
|
assert.equal(actual, 2);
|
||||||
});
|
});
|
||||||
|
it('returns zero for no match with non-global regular expression', function() {
|
||||||
|
var r = /s/g;
|
||||||
|
var str = 'no match';
|
||||||
|
var actual = emoji.getCountOfAllMatches(str, r);
|
||||||
|
assert.equal(actual, 0);
|
||||||
|
});
|
||||||
|
it('returns 1 for match with non-global regular expression', function() {
|
||||||
|
var r = /s/;
|
||||||
|
var str = 's + s';
|
||||||
|
var actual = emoji.getCountOfAllMatches(str, r);
|
||||||
|
assert.equal(actual, 1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasNormalCharacters', function() {
|
describe('hasNormalCharacters', function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue