How to replace occurences in string using groups in Swift?

后端 未结 1 1251
渐次进展
渐次进展 2021-01-28 08:05

I simply need to replace:

,

with

\\n

, \\n

in string

相关标签:
1条回答
  • 2021-01-28 08:52

    You can do a regular expression search, with a template in the replacement string:

    let string = "<p>hello</p> my <div>Doggy</div>"
    let newString = string.replacingOccurrences(of: "<p>|<div>", with: "\n$0", options: .regularExpression)
    

    For each match, the $0 template is replaced by what actually matched the pattern.

    0 讨论(0)
提交回复
热议问题