Swift Regex for case insensitive search

Hi,

Aim

  • I would like to use Swift Regex to find a replace a string.
  • I would like it to be a case insensitive search.

Code

var s1 = "THis is a LONG sentence"
s1.replace("this", with: "hello") //I would like it to replace "THis" with "hello"

Questions

  1. How can achieve this? (Preferably in Swift Regex)
  2. Or is there a workaround?
var s1 = "THis is a LONG sentence"
s1.replace(try! Regex("this").ignoresCase(), with: "hello")
print(s1) // hello is a LONG sentence
1 Like

@cukr Thank you so much!!!

I should have checked the Regex API

Really loving Swift Regex!

1 Like