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...
- 2janlelis/unicode-emoji: Up-to-date Emoji Regex in Ruby - GitHub
require "unicode/emoji" string = "String which contains all kinds of emoji: - Singleton Emoji: 😴 ...
- 3😃 Every Emoji by Codepoint - Emojipedia
😃 Every Emoji by Codepoint. 😀 Grinning Face, U+1F600. 😃 Grinning Face with Big Eyes, U+1F603.
- 4Emoji unicode characters for use on the web - Experimental ...
- 5How to get all available emojis in an array in Swift - Apple ...
I want to be able to get an array of all the available emojis in Swift. ... ://stackoverflow.com/...