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
How to blur an image in Golang?
How can I convert a string variable into Boolean, Integer or Float type in Golang?
Golang HTML parser
How to use array in Go Programming Language?
How to check if a string contains certain characters in Golang?
How to play and pause execution of goroutine?
How to read/write from/to file in Golang?
Example of Switch Case with Break in For Loop
Print index and element or data from Array, Slice and Map
Anonymous Functions in Golang
Most Helpful This Week
How to read names of all files and folders in current directory?How can we reverse a simple string in Go?Closures Functions in GolangReplace numbers by zero from stringExample to handle GET and POST request in GolangRegular expression to extract all Non-Alphanumeric Characters from a StringHow to convert Go struct to JSON?How to Remove duplicate values from Slice?How to create Empty and Nil Slice?Get current date and time in various format in golang