Last active
January 27, 2021 14:30
-
-
Save mm580486/8102d3622ee5ec7e150b35923e9a69f7 to your computer and use it in GitHub Desktop.
remove emoji from string - ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# method base | |
def clean_emoji(str='') | |
str=str.force_encoding('utf-8').encode | |
arr_regex=[/[\u{1f600}-\u{1f64f}]/,/[\u{2702}-\u{27b0}]/,/[\u{1f680}-\u{1f6ff}]/,/[\u{24C2}-\u{1F251}]/,/[\u{1f300}-\u{1f5ff}]/] | |
arr_regex.each do |regex| | |
str = str.gsub regex, '' | |
end | |
return str | |
end | |
# sample | |
clean_emoji "i'm hungry 😭" #=>"i'm hungry" | |
#module base | |
module StringHelper | |
def clean_emoji | |
str = self.force_encoding('utf-8').encode | |
arr_regex=[/[\u{1f600}-\u{1f64f}]/,/[\u{2702}-\u{27b0}]/,/[\u{1f680}-\u{1f6ff}]/,/[\u{24C2}-\u{1F251}]/,/[\u{1f300}-\u{1f5ff}]/] | |
arr_regex.each do |regex| | |
str = str.gsub regex, '' | |
end | |
return str | |
end | |
end | |
class String | |
include StringHelper | |
end | |
# sample | |
"i'm hungry 😭".clean_emoji #=>"i'm hungry" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It removes Chinese as well...