github.com/wormhole-foundation/wormhole-explorer/common@v0.0.0-20240604151348-09585b5b97c5/utils/domain.go (about) 1 package utils 2 3 import ( 4 "regexp" 5 "strings" 6 ) 7 8 // FindSubstringBeforeDomains finds the substring before specified domains appear 9 // and also removes http:// or https:// prefix from the string. 10 func FindSubstringBeforeDomains(s string, domains []string) string { 11 // Regular expression to match the protocol prefix (http:// or https://) 12 reProtocol := regexp.MustCompile(`^(http://|https://)`) 13 // Remove the protocol prefix 14 s = reProtocol.ReplaceAllString(s, "") 15 16 // Create a regular expression pattern for the domains 17 // Escape dots since they have a special meaning in regex, and join domains with | 18 domainPattern := strings.Join(domains, "|") 19 domainPattern = strings.Replace(domainPattern, ".", `\.`, -1) 20 reDomain := regexp.MustCompile("(" + domainPattern + ")") 21 22 // Find the index of the first occurrence of any domain 23 loc := reDomain.FindStringIndex(s) 24 if loc != nil { 25 // Return the substring from the start to the first domain occurrence 26 return s[:loc[0]] 27 } 28 29 // If no domain is found, return the original modified string 30 return s 31 }