How to check if a string contains emojis in JavaScript?

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?