Skip to content

Instantly share code, notes, and snippets.

@cmkilger
Last active March 28, 2018 21:28
Show Gist options
  • Save cmkilger/a0d097fc04b2d4ceef17 to your computer and use it in GitHub Desktop.
Save cmkilger/a0d097fc04b2d4ceef17 to your computer and use it in GitHub Desktop.
Detects if a string is made up of just emoji (and space).
import Foundation
extension String {
private static let regex = try! NSRegularExpression(pattern: "^[\\s\n\r]*(?:(?:[\u{a9}\u{ae}\u{203c}\u{2049}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21a9}-\u{21aa}\u{231a}-\u{231b}\u{2328}\u{23cf}\u{23e9}-\u{23f3}\u{23f8}-\u{23fa}\u{24c2}\u{25aa}-\u{25ab}\u{25b6}\u{25c0}\u{25fb}-\u{25fe}\u{2600}-\u{2604}\u{260e}\u{2611}\u{2614}-\u{2615}\u{2618}\u{261d}\u{2620}\u{2622}-\u{2623}\u{2626}\u{262a}\u{262e}-\u{262f}\u{2638}-\u{263a}\u{2648}-\u{2653}\u{2660}\u{2663}\u{2665}-\u{2666}\u{2668}\u{267b}\u{267f}\u{2692}-\u{2694}\u{2696}-\u{2697}\u{2699}\u{269b}-\u{269c}\u{26a0}-\u{26a1}\u{26aa}-\u{26ab}\u{26b0}-\u{26b1}\u{26bd}-\u{26be}\u{26c4}-\u{26c5}\u{26c8}\u{26ce}-\u{26cf}\u{26d1}\u{26d3}-\u{26d4}\u{26e9}-\u{26ea}\u{26f0}-\u{26f5}\u{26f7}-\u{26fa}\u{26fd}\u{2702}\u{2705}\u{2708}-\u{270d}\u{270f}\u{2712}\u{2714}\u{2716}\u{271d}\u{2721}\u{2728}\u{2733}-\u{2734}\u{2744}\u{2747}\u{274c}\u{274e}\u{2753}-\u{2755}\u{2757}\u{2763}-\u{2764}\u{2795}-\u{2797}\u{27a1}\u{27b0}\u{27bf}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{2b50}\u{2b55}\u{3030}\u{303d}\u{3297}\u{3299}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{1f191}-\u{1f19a}\u{1f201}-\u{1f202}\u{1f21a}\u{1f22f}\u{1f232}-\u{1f23a}\u{1f250}-\u{1f251}\u{200d}\u{1F300}-\u{1F5FF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}\u{e0020}-\u{e007f}]|\u{200d}[\u{2640}\u{2642}]|[\u{1f1e6}-\u{1f1ff}]{2}|.[\u{20e0}\u{20e3}\u{fe0f}]+)+[\\s\n\r]*)+$", options: [])
var isEmojiOnly: Bool {
return (String.regex.firstMatchInString(self, options: [], range: NSMakeRange(0, (self as NSString).length)) != nil)
}
}
func test(string: String, _ isEmoji: Bool) {
if string.isEmojiOnly != isEmoji {
print("\(string) should\((isEmoji ? "" : " not")) be emoji only")
}
}
test("Hello ๐Ÿ˜ฒ", false)
test("-", false)
test("+", false)
test("$", false)
test("๐Ÿ‘ด๐Ÿป", true) // Requires Unicode 8.0 support for skin tones
test(" ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง ", true)
test(" 0๏ธโƒฃ โค๏ธ", true)
test("๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง โค๏ธ", true)
test(" 0๏ธโƒฃ โค๏ธ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง ", true)
test("0๏ธโƒฃ\nโค๏ธ", true)
test("ยฉยฎ๐Ÿ”๐Ÿ”›๐Ÿ”™๐Ÿ’ฑโž•โž–โž—ยฎยฉโ„ข๐Ÿ”œ๐Ÿ”ด๐Ÿ”ตโšซ๏ธโšช๏ธ๐Ÿ”น๐Ÿ”˜๐Ÿ”ธโ–ช๏ธโ–ซ๏ธโ—ป๏ธโ—พ๏ธ๐Ÿ”‰๐Ÿ”‡โ™ฆ๏ธโ™ฅ๏ธโ™ฃ๏ธโ™ ๏ธ๐Ÿ€„๏ธ๐ŸŽด๐Ÿ‘โ€๐Ÿ—จ๐Ÿ’ญ๐Ÿ—ฏ๐Ÿ•”๐Ÿ•“๐Ÿ•–๐Ÿ•–๐Ÿ•œ๐Ÿ•ฅ๐Ÿ’ฌ๐Ÿ•Žโ˜ชโ˜ช๐Ÿ”โ˜‘๏ธ#๏ธโƒฃ#๏ธโƒฃโคต๏ธ*๏ธโƒฃโ„น๏ธโ†ฉ๏ธโ†ช๏ธโ†™๏ธโ†˜๏ธโžก๏ธ๐Ÿ”‚โ—€๏ธ๐Ÿ”ผโธโฏ4๏ธโƒฃ๐Ÿ†“๐Ÿ†–๐Ÿ“ถ๐Ÿ†’๐Ÿ›„๐Ÿง๐ŸŒ€๐Ÿ’ โœ…ใ€ฝ๏ธ๐Ÿ’ฏโ‰๏ธโ•๐Ÿšณ๐Ÿšฏ๐Ÿšท๐ŸšซโŒโญ•๏ธ๐Ÿ’ขโ›”๏ธ๐Ÿ…ฑใŠ™๏ธโœด๏ธ๐Ÿ†š๐Ÿ’˜๐Ÿ’•๐Ÿ’š๐Ÿ”Ž๐Ÿ“๐Ÿ”“๐Ÿšฉ๐ŸŽ๐ŸŽ€โ›ฑ๐Ÿ›๐Ÿ—ฟ๐Ÿ”‘๐Ÿท๐Ÿ”ฌ๐Ÿบ๐Ÿ›กโš”๐Ÿ”ซ๐Ÿ› ๐Ÿ› ๐Ÿ’Ž๐Ÿ’ด๐Ÿ•ฏ๐Ÿ“กโฒ๐Ÿ“ป๐Ÿ“ž๐Ÿ“ธ๐Ÿ’พ๐Ÿ–ฑ๐Ÿ“ฒ๐Ÿ•๐Ÿฏ๐ŸŽ‡๐Ÿ™๐Ÿœ๐Ÿž๐ŸŽข๐Ÿšฅ๐Ÿšฆ๐Ÿ’บ๐Ÿšค๐Ÿš‡๐Ÿš…๐ŸšŸ๐Ÿš๐Ÿšœ๐Ÿš‘๐Ÿš™๐ŸŽท๐ŸŽบ๐Ÿšด๐Ÿพ๐ŸŽฑ๐Ÿ‰๐Ÿ€๐Ÿถ๐Ÿต๐Ÿฟ๐ŸŽ‚๐Ÿก๐Ÿ›๐ŸŒญ๐Ÿ ๐Ÿ๐Ÿ‡โ›„๏ธโ˜ƒ๐ŸŒจโ˜€๏ธโ˜„โญ๏ธ๐ŸŒ๐ŸŒš๐ŸŒš๐ŸŒ๐ŸŒ‘๐ŸŒ’๐ŸŒ•๐ŸŒบ๐Ÿš๐Ÿ•ธ๐Ÿƒ๐ŸŒด๐Ÿ‘๐Ÿ„๐Ÿ„๐ŸŒ๐Ÿฆ๐Ÿ—๐Ÿฝ๐Ÿญ๐Ÿ’๐Ÿ•ถ๐Ÿ‘“๐Ÿ‘ ๐Ÿ‘ž๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ‘ฉ๐Ÿ™Ž๐Ÿผ๐Ÿ’‚๐Ÿฝ๐Ÿ‘ฅ๐Ÿ‘‡๐Ÿป๐Ÿ‘๐Ÿป๐Ÿ˜ฟ๐Ÿค•๐Ÿ˜ฒ๐Ÿ˜ช๐Ÿ™๐Ÿ˜Ÿ๐Ÿ™„๐Ÿ˜˜๐Ÿ˜๐Ÿ‘๐ŸŒด๐Ÿ‘“๐Ÿ‘๐Ÿป๐Ÿ˜ฒ๐Ÿ˜˜", true)
test("ใŽ", false)
test("ใท", false)
test("ไท‘", false)
test("ไทซ", false)
test("ไทฝ", false)
test("๊’ž", false)
@cmkilger
Copy link
Author

Added support for flag tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment