JS emoji expression length judgment

Source: Internet
Author: User

1. Requirements: The input frame length is limited to 10 characters, including the expression. The length hint is exceeded.

Note: The iphone custom expression, there are four villain, three villain, mainly the length is not the same. Some expressions may exceed the length limit (10), such as??????????? This expression length is 11, of course, I set the length of the relatively small, can be arbitrarily set.

The process of accessing the data found that Lodash can handle arrays and can handle emoticons.

Console.log (_.toarray (' 12??? '). length); -3);

That would be in line with the conditions I intercepted. Determines if one of the array's lengths exceeds my limit length, the array is truncated.

This would have been possible, but it would not be appropriate to introduce a file for this purpose. So I put the source of some of the content in line with my needs to pick out.

Let Rsastralrange = ' \\ud800-\\udfff ',
RSZWJ = ' \\u200d ',
Rsvarrange = ' \\ufe0e\\ufe0f ',
Rscombomarksrange = ' \\u0300-\\u036f ',
Recombohalfmarksrange = ' \\ufe20-\\ufe2f ',
Rscombosymbolsrange = ' \\u20d0-\\u20ff ',
Rscomborange = rscombomarksrange + Recombohalfmarksrange + rscombosymbolsrange;
Let Rehasunicode = RegExp (' [' + RSZWJ + rsastralrange + rscomborange + rsvarrange + '] ');

Let Rsfitz = ' \\ud83c[\\udffb-\\udfff] ',
Rsoptvar = ' [' + Rsvarrange + ']? ',
Rscombo = ' [' + Rscomborange + '] ',
Rsmodifier = ' (?: ' + Rscombo + ' | ' + Rsfitz + ') ',
Reoptmod = Rsmodifier + '? ',
rsastral = ' [' + Rsastralrange + '] ',
rsnonastral = ' [^ ' + rsastralrange + '] ',
Rsregional = ' (?: \ \UD83C[\\UDDE6-\\UDDFF]) {2} ',
Rssurrpair = ' [\\ud800-\\udbff][\\udc00-\\udfff] ',
Rsoptjoin = ' (?: ' + Rszwj + ' (?: ' + [Rsnonastral, rsregional, Rssurrpair].join (' | ') + ') ' + Rsoptvar + reoptmod + ') * ',
Rsseq = Rsoptvar + Reoptmod + rsoptjoin,
Rssymbol = ' (?: ' + [rsnonastral + Rscombo + '? ', Rscombo, rsregional, Rssurrpair, Rsastral].join (' | ') + ') ';
Let Reunicode = RegExp (Rsfitz + ' (? = ' + Rsfitz + ') | ' + Rssymbol + rsseq, ' G ');

Export function ToArray (val) {//String to array
Return Hasunicode (Val)
? Unicodetoarray (Val)
: Asciitoarray (Val);
}

Export function Hasunicode (val) {
Return Rehasunicode.test (Val);
}

Export function Unicodetoarray (val) {
Return Val.match (reunicode) | | [];
}

Export function Asciitoarray (val) {
Return Val.split (");
}

Yes, a lot of regular. It's a headache to watch. This method can be satisfied, the expression is transferred to a portion of the group. In this case, we can only use the judgment array.
Then according to the resulting array, go to each one to determine the length of each; The code is as follows:
Let Strarr = Utils.toarray (val);
Let str = ", strlen = 0, num = 0, Lastarr = [];
for (Let i = 0;i < strarr.length;i++) {
Strlen + = Strarr[i].length; The length of each character in the array is added
if (Strlen > This.limit) {//If the length is greater than the limit
num = i; Record down to the beginning of the limit beyond the first
Lastarr = Strarr.slice (0, This.limit); Remove an array within the limit
this. $toast (' length exceeded ');
Break Exit loop
Return
}
}
if (Strlen > This.limit) {//If the first number of input is greater than the expression of the limit
for (var j = 0;j < num;j++) {
str + = Lastarr[j]; Remove the character of the array within the limit
}
This.currentvalue = str;
}

In fact, this block can also encapsulate a method call.

JS emoji expression length judgment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.