我有以下用户名
:iphone6s_taylor
和iphone6+_taylor
。
为了在我创建的以下正则表达式的文本中找到它们:
let regex = "\\B@\(userName)\\b"
但在以下文本中:
let text = "@iphone6s_taylor @iphone6+_taylor @iphone6+_taylor"
仅找到第一个。如何更改正则表达式来找到它们中的每一个?
最佳答案
您可以使用escapedPatternForString
:
Returns a string by adding backslash escapes as necessary to the given string, to escape any characters that would otherwise be treated as pattern metacharacters.
您的场景的示例用法:
let userName = NSRegularExpression.escapedPatternForString("iphone6+_taylor")
关于ios - 如何排除 "+"在 NSRegularExpression 中被视为特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652174/