Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138221

Global flag in javascript

$
0
0

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?


Viewing all articles
Browse latest Browse all 138221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>