How to count the number of Emoji in the string?

Hello!

So check Emoji in the title:

isEmoji function() {
 var str = document.getElementById('Title').value;

 var ranges = [
 '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
 '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
 '\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];
 if (str.match(ranges.join('|'))) {
 return alert('1');
 } else {
 return alert('0');
}
 }


But how else would count the number of these Emoji?
June 10th 19 at 15:08
1 answer
June 10th 19 at 15:10
Solution
If you check the match of the regular season with the g flag (global) that match returns the matching elements in an array, the length of which will show number of found substrings.

47dd07fd70c3933e2dac8.png

This option will return the number of found features, respectively. it is also possible to test for true|false at the exit.

isEmoji function() {

 var str = document.getElementById('Title').value;

 var ranges = [
 '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
 '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
 '\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];

 return str.match(new RegExp(ranges.join('|'), 'g')).length;
}
Thank you helped, what you need.

$('#Title').change(function(){


 var str = document.getElementById('Title').value;
 var lngt = 0;
 var ranges = [
 '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
 '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
 '\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];

 lngt = str.match(new RegExp(ranges.join('|'), 'g')).length;

$('#Emogi').val(lngt);


 });
- domenico_Witti commented on June 10th 19 at 15:13
please :)

PS. you are confused with such names as the variables lngt ? Would it not be simpler to call emojiCount ? Well it is a matter of taste and readability.

Good luck in the development - audrey_Kautz commented on June 10th 19 at 15:16
agree, well, it's the rush to distribute to verify that, of course it is necessary correctly. Thank you, same to you. - domenico_Witti commented on June 10th 19 at 15:19

Find more questions by tags JavaScript