The regexp.FindStringIndex(s string, n int) []int function returns byte indices of matches. In simple scenarios, these locations correspond to the "character position" in the string. However, certain characters foil this assumption. For example:
package main
import (
"fmt"
"regexp"
)
var (
re = regexp.MustCompile(`bbb`)
str1 = "aaa bbb ccc"
str2 = "aaa✌️bbb ccc"
)
func main() {
fmt.Println(str1, re.FindStringIndex(str1))
fmt.Println(str2, re.FindStringIndex(str2))
}
Result:
aaa bbb ccc [4 7]
aaa✌️bbb ccc [9 12]
Why is this and how could one convert the FindStringIndex result to locate characters within a string rather than bytes?
EDIT: To be clear, in my specific use case these character indices are being sent to Javascript to manipulate HTML, and the JS needs to know the offsets of substrings in terms of characters, not bytes. If further manipulation were happening in Go it would be easy to slice into the strings using the raw results of FindStringIndex, but this is not the case.