利用正则找出[哈哈]emoji

 attributedTextWithText(text : String)->NSMutableAttributedString?{
        // 表情正则
        let pattern = "\\[.*?\\]"
        guard let regularExpression = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
        let textCheckingResults = regularExpression.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
        if textCheckingResults.count == 0 {
            
            return nil
        }
        
        for result in textCheckingResults{
            let te = (text as NSString).substringWithRange(result.range)
            print(te)
        }
        
        return nil
    }

调用

override func viewDidLoad() {
        super.viewDidLoad()
        
        let emoji = EmojiRegular()
        
        emoji.attributedTextWithText("[哈哈]2123[嘻嘻]")
    }

你可能感兴趣的:(利用正则找出[哈哈]emoji)