如果你有一个需要识别字符串的段落,那么你可以使用两个正则表达式来实现它: namePattern := `\[(\w+)\]`replacerRegex := regexp.MustCompile(namePattern)finderRegex := regexp.MustCompile("hello " + namePattern + ", I'm " + namePattern)fmt.Println(re.ReplaceAllString(re1.FindString("hi hih hi hello [Jim], I'm [Sam]"), "[MR $1]")) https://go.dev/play/p/kv6CfTv0-sk EDIT: 保留字符串其他部分的简单方法(PS:可以优化,需要检查边缘情况) str := "pre string hello [Jim], I'm [Sam] post string"namePattern := `\[(\w+)\]`finderRegex := regexp.MustCompile("hello " + namePattern + ", I'm " + namePattern)replacerRegex := regexp.MustCompile(namePattern)// string part subject to re