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: Stack and Caller from RUNTIME package
Different ways to convert Byte Array into String
Get Year, Month, Day, Hour, Min and Second from current date and time.
Example: Fields and FieldsFunc from BYTES Package
Copy an array by value and reference into another array
How to blur an image in Golang?
Most Helpful This Week
How to replace emoji characters in string using regex in Golang?Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]Different ways to convert Byte Array into StringHow to iterate over an Array using for loop?Find capacity of Channel, Pointer and SliceVarious examples of Carbon date-time package in GolangHow to read/write from/to file in Golang?How to Unmarshal nested JSON structure?Regular expression to extract all Non-Alphanumeric Characters from a StringHow can we reverse a simple string in Go?