How to check if a string contains emojis in JavaScript?
文章推薦指數: 80 %
This range of Unicode will match almost all the emoji's in a string. // Regular expression to match emoji const regexExp = /(\u00a9|\u00ae|[\ ... MelvinGeorgeBlogAboutMENUHowtocheckifastringcontainsemojisinJavaScript?PublishedFebruary15,2021TocheckifastringcontainsemojisinJavaScript,wecanusearegexexpressiontomatcharangeofUnicodespecifictoemojis.TL;DR//Matchalmostallemoji conststr="Hello😃😄"; /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi.test( str );Fordetailedexplanation.Readon📖.Thisistheregexexpressionformatchingalmostalltheemojisinastring.ThisrangeofUnicodewillmatchalmostalltheemoji'sinastring.//Regularexpressiontomatchemoji constregexExp=/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi;Nowlet'swriteastringwithsomeemojis.//Regularexpressiontomatchemoji constregexExp=/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi; //Stringwith3emojiandsomeletters conststr="Hello😃😄";Nowtotestthestring,wecanusethetest()methodavailableintheregularexpressionwedefined.Itcanbedonelikethis,//Regularexpressiontomatchemoji constregexExp=/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi; //Stringwith3emojiandsomeletters conststr="Hello😃😄"; regexExp.test(str);//trueThetest()methodwillacceptastringtypeasanargumenttotestforamatch.Themethodwillreturnbooleantrueifthereisamatchusingtheregularexpressionandfalseifnot.SeetheaboveexampleliveinJSBin.Feelfreetoshareifyoufoundthisuseful😃.Shareon:FacebookTwitter GoBack
延伸文章資訊
- 1What is the regex to extract all the emojis from a string? - Stack ...
Using emoji-java i've wrote a simple method that removes all emojis including fitzpatrick modifie...
- 2Python program to print Emojis - GeeksforGeeks
Every emoji has a Unicode associated with it. Emojis also have a CLDR short name, which can also ...
- 3Emoji unicode characters for use on the web - Experimental ...
- 4How to check if a string contains emojis in JavaScript?
This range of Unicode will match almost all the emoji's in a string. // Regular expression to mat...
- 5Emojis in Your Data
Everything you need to know about emoji in your database or dataset. ... Adapting to every emoji ...