Filter the input of emoji expression with JS

Source: Internet
Author: User

This article is original, reprint please indicate source: CNZT article: Cnzt-p

Http://www.cnblogs.com/zt-blog/p/6773854.html

In the front-end page development process, will always encounter the need to not allow input box input emoji expression, my idea is to use regular matching expression by encoding, and then replace it with a null character creation. But the question is also obvious, what is the complete set of encodings? Check out the official documentation and find that it doesn't give you the answers you want. And a lot of emoji expressions in addition to the main code and the sub-code (this is my name), for example:

\UD83C\UDC00 is an expression, \ud83c\udc00\u200d is an expression, I put \ud83c\udc00 called the main code, \u200d called the sub-code (or derivative code, anyway the meaning of understanding the line).

--------------------------the next long process of manually testing the encoding set--------------------------

The final solution is as follows:

1 var regstr =/[\ud83c|\ud83d|\ud83e][\udc00-\udfff][\u200d|\ufe0f]| [\ud83c|\ud83d|\ud83e] [\udc00-\udfff]| [0-9|*|#]\ufe0f\u20e3| [0-9|#]\u20e3| [\u203c-\u3299]\ufe0f\u200d| [\u203c-\u3299]\ufe0f| [\u2122-\u2b55]|\u303d| [\a9|\ae]\u3030|\ua9|\uae|\u3030/IG; 2 var org_val = $ ("input"). Val (); 3 4 if (Regstr.test (org_val)) {5 $ ("#input"). Val (Org_val.replace (Regstr, "")); 6 }

The above can match iOS10.2.1 and all the previous emoji expression, Android's expression because more, such as the above is missing the regular, can be supplemented by themselves. The encoding method for testing an expression is as follows:

1 <input type= "text"/>234var org_val = $ ("input"). Val (); 5 Console.log (Escape (Org_val)); 6 // escape to get%ud83c%u ... Format encoding, can correspond to \u ... Format

The end of this article.

Filter the input of emoji expression with JS

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.