function countSmileys(arr) {
let regex = /^([:;])([-~])*([)D])$/
if(arr.length === 0) {
return 0;
}
return arr.filter(el => {
return regex.test(el);
})
}
console.log(countSmileys([':(', ';)', ':)', ';o>', ':(', ':>', ';~D']));
I wrote the code that verify to see if that is smiley face or not. for eyes, it takes only ':/;' , for nose, '-/~' (it is fine if there is a nose) for lips, ')/D'.
So, I wrote the regular expression with global flag first. then it only gives
[';)', ';~D']
I finally got the right result when I eliminated g flag.
[';)', ':)', ';~D']
I have been confused to use g flags. can anyone explain this?