how to replace occurrence of more than one string using "replacingOccurrences(of:"

Viewed 42

I am a beginner developer and I want to replace occurrence of both gmail.com and icloud.com. Ive tried using:

let email =
            vm.chatUser?.email.replacingOccurrences(of:
            "@gmail.com", "@icloud.com", with: "") ?? ""

but its giving me an error.

1 Answers

To search for both strings you need to use a regular expression with an OR, |, condition. The regex pattern (a|b) means find 'a' or 'b'.

email?.replacingOccurrences(of: "(@gmail.com|@icloud.com)", 
                            with: "", 
                            options: .regularExpression) ?? "" 
Related