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 use for and foreach loop?
Select single argument from all arguments of variadic function
Regular Expression to get a string between parentheses in Golang
How to extract text from between html tag using Regular Expressions in Golang?
How to check if a string contains certain characters in Golang?
Higher Order Functions in Golang
Most Helpful This Week
How to create Map using the make function in Go?Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]How to find the type of the variable by different ways in Golang?Different ways to convert Byte Array into StringHow to Remove duplicate values from Slice?Sample program to create csv and write dataGolang Get current Date and Time in EST, UTC and MST?How to create Empty and Nil Slice?Example: Fields and FieldsFunc from BYTES PackageHow can we reverse a simple string in Go?