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
Regular expression to extract domain from URL
Different ways to validate JSON string
Various examples of printing and formatting in Golang
Different ways to convert Byte Array into String
Regular expression to extract text between square brackets
How to check UPPERCASE characters in a string in Golang?
Most Helpful This Week
Convert Float32 to Float64 and Float64 to Float32Regular expression to extract text between square bracketsGet Set and Clear Session in GolangExample: How to use ReadAtLeast from IO Package in Golang?How to get Dimensions of an image type jpg jpeg png or gif ?How to use a mutex to define critical sections of code and fix race conditions?Split URL and Get Parameters from URLRegular expression to extract numbers from a string in GolangWhat is GOPATH?Golang Get current Date and Time in EST, UTC and MST?