Case insensitive string replace

824 views
Skip to first unread message

Tong Sun

unread,
Jul 10, 2015, 3:22:10 PM7/10/15
to golan...@googlegroups.com
Hi, 

Can NewReplacer.Replace do case insensitive string replace?

r := strings.NewReplacer("html", "xml")
fmt.Println(r.Replace("This is <b>HTML</b>!"))

If not, what's the best way to do case insensitive string replace in Go?

Thanks

Tong Sun

unread,
Jul 10, 2015, 3:30:18 PM7/10/15
to golang-nuts

But unfortunately, it can work with string matching but can't work with string replacing -- because when doing string replacing, we only want to change the part that need to be changed. 

---------- Forwarded message ----------
From: andrey mirtchovski <mirtc...@gmail.com>
Date: Fri, Jul 10, 2015 at 3:26 PM

use strings.ToLower() on the original string and the match.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Matt Harden

unread,
Jul 10, 2015, 3:50:22 PM7/10/15
to Tong Sun, golang-nuts
regexp.MustCompile(`(?i)html`).ReplaceAllLiteralString("This is <b>HTML</b>!", "xml")
Reply all
Reply to author
Forward
0 new messages