问题
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