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
Example: How to use ReadFull from IO Package in Golang?
How to fetch an Integer variable as String in Go?
How to iterate over a Map using for loop in Go?
How to fix race condition using Atomic Functions in Golang?
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
How to trim leading and trailing white spaces of a string in Golang?
Most Helpful This Week
Normal function parameter with variadic function parameterHow to get Dimensions of an image type jpg jpeg png or gif ?Golang Convert String into Snake CaseHow to get struct variable information using reflect package?How to read names of all files and folders in current directory?How to check lowercase characters in a string in Golang?How to create Empty and Nil Slice?The return values of a function can be named in GolangCatch values from GoroutinesExample of Sscan vs Sscanf vs Sscanln from FMT Package