convert javascript regex expression to swift nsregularexpression

混江龙づ霸主 提交于 2019-12-11 04:46:25

问题


I am new to swift programming.

I have a valid regex in javascript, I need the same functionality in swift

str.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');

Thanks


回答1:


Swift 2.3:

let str = "A+B/C===="

let result = str.stringByReplacingOccurrencesOfString("+", withString: "-")
                .stringByReplacingOccurrencesOfString("/", withString: "_")
                .stringByReplacingOccurrencesOfString("\\=+$", withString: "", options: .RegularExpressionSearch)

print(str)
print(result)

Swift 3:

let str = "A+B/C===="

let result = str.replacingOccurrences(of: "+", with: "-")
                .replacingOccurrences(of: "/", with: "_")
                .replacingOccurrences(of: "\\=+$", with: "", options: .regularExpressionSearch)

print(str)
print(result)


来源:https://stackoverflow.com/questions/37857655/convert-javascript-regex-expression-to-swift-nsregularexpression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!