Since an English or Arabic letter regex (as described in this answer you linked to, also, see this answer, too) is [a-zA-Za-z\u0621-\u064A]
and an English or Arabic digit regex is [0-9\u0660-\u0669]
you may use
let passwordRegex = "^(?=.*[a-zA-Z\\u0621-\\u064A])(?=.*[0-9\\u0660-\\u0669])[a-zA-Za-z\\u0621-\\u064A0-9\\u0660-\\u0669]{8,}$"
NOTE: you do not need the outer ^
and $
anchors because MATCHES
requires the pattern to match the whole string input.
Another way to match an Arabic letter with ICU regex used in Swift is to use [\p{L}&&[\p{script=Arabic}]]
(it is an intersection inside a character class, it matches any letter but from the Arabic character set). Same with a digit: [\p{N}&&[\p{script=Arabic}]]
. Then, the regex will look like
let passwordRegex = "^(?=.*[\\p{L}&&[\\p{script=Arabic}A-Za-z]])(?=.*[\\p{N}&&[\\p{script=Arabic}0-9]])[\\p{L}\\p{N}&&[\\p{script=Arabic}a-zA-Z0-9]]{8,}$"
So, here
[\\p{L}&&[\\p{script=Arabic}A-Za-z]]
- any letter but it should belong to either ASCII letters or Arabic script
[\\p{N}&&[\\p{script=Arabic}0-9]]
- any digit but either from 0-9
range or Arabic script
[\\p{L}\\p{N}&&[\\p{script=Arabic}a-zA-Z0-9]]
- any letter or digit but only from the ASCII 0-9
, A-Z
, a-z
and Arabic script.
Note also, that in order to match any letters, you may use\p{L}
and to match any digits you may use \d
(they are Unicode aware in ICU library). So, *in case t does not matter if the letters or digits are Arabic, English, Greek or whatever, you may use
let passwordRegex = "^(?=.*\\p{L})(?=.*\\d)[\\p{L}\\d]{8,}$"