In Swift, how can I check if a String
is alphanumeric, ie, if it contains only one or more alphanumeric characters [a-zA-Z0-9]
, excluding letters with diacritics, eg, é.
extension String {
var isAlphanumeric: Bool {
return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
}
}
"".isAlphanumeric // false
"abc".isAlphanumeric // true
"123".isAlphanumeric // true
"ABC123".isAlphanumeric // true
"iOS 9".isAlphanumeric // false