How to replace emoji characters in string using regex in Golang?
Replace emoji characters in String
In the following program ReplaceAllString() method is used, which allows us to replace original string with another string if the specified string matches with the specified regular expression. This method is defined under the regexp package, hence to access ReplaceAllString() method first we need to import the regexp package in our program.
Example
package main
import (
"fmt"
"regexp"
)
func main() {
var emojiRx = regexp.MustCompile(`[\x{1F600}-\x{1F6FF}|[\x{2600}-\x{26FF}]`)
var str = emojiRx.ReplaceAllString("Thats a nice joke 😆😆😆 😛", `[e]`)
fmt.Println(str)
}
Output
Thats a nice joke 😆😆😆 😛
Most Helpful This Week
Split a string at uppercase letters using regular expression in Golang
Regular expression to extract text between square brackets
Regular Expression to get a string between parentheses in Golang
Regular expression to split string on white spaces
Regular expression to validate the date format in "dd/mm/yyyy"
Regular expression to extract all Non-Alphanumeric Characters from a String
Most Helpful This Week
Get Year, Month, Day, Hour, Min and Second from a specified dateGolang download image from given URLHow to include and execute HTML template?Dynamic JSON parser without Struct in GolangGolang Functions Returning Multiple ValuesHow to fix race condition using Atomic Functions in Golang?Generate a Keygen of 256 bitsHow to write backslash in Golang string?Passing multiple string arguments to a variadic functionReplace first occurrence of string using Regexp