简体   繁体   English

正则表达式匹配仅带有表情符号而没有其他文本的文本

[英]Regex to match text with only emoticons and no other text

I have this regex for detecting if a string contains any emoticons:我有这个正则表达式来检测一个字符串是否包含任何表情符号:

/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|[\ud83c[\ude50\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g

But how can I detect if a string ONLY contains emoticons and not any other standard text?但是如何检测字符串是否仅包含表情符号而不包含任何其他标准文本? I tried to enclose the above in [] without success.我试图将以上内容包含在[]但没有成功。 Am using javascript.我正在使用 javascript。

Remove anything that matches the pattern and see if there's anything left.删除与模式匹配的任何东西,看看是否还有任何东西。 Consider using trim() as well if you think there might be unwanted whitespace.如果您认为可能存在不需要的空格,请考虑使用trim()

 var pattern = /(?:[\✀-\➿]|(?:\?[\?-\?]){2}|[\?-\?][\?-\?]|[\#-\9]\️?\⃣|\㊙|\㊗|\〽|\〰|\Ⓜ|\?[\?-\?]|\?[\?-\?]|\?\?|\?[\?-\?]|\?[\?-\?]|[\?[\?\?]|\?[\?-\?]|\?\?|\?\?|[\?[\?\?]|\?\?|\?\?|\?[\?-\?]|[\?[\?\?]|\?[\?-\?]|\‼|\⁉|[\▪-\▫]|\▶|\◀|[\◻-\◾]|\©|\®|\™|\ℹ|\?\?|[\☀-\⛿]|\⬅|\⬆|\⬇|\⬛|\⬜|\⭐|\⭕|\⌚|\⌛|\⌨|\⏏|[\⏩-\⏳]|[\⏸-\⏺]|\?\?|\⤴|\⤵|[\←-\⇿])/g; var str2 = 'ℹ😂〰'; var str1 = 'ℹ😂〰asdfasd'; var isOnlyEmojis = str=>str.replace(pattern, '') === ""; console.log(isOnlyEmojis(str2) ? "It's just emojis!" : "Has real text..") console.log(isOnlyEmojis(str1) ? "It's just emojis!" : "Has real text..")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM