22

I have a question: I want to do a validation for the first and the last name with RegEx. I want to do it with only Hebrew and English without numbers. Someone can help me to do that code?

Jason C
  • 38,729
  • 14
  • 126
  • 182
iYonatan
  • 916
  • 3
  • 10
  • 26
  • This seems like a good place for this link: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Biffen Jul 31 '14 at 20:31

8 Answers8

47

Seemingly Hebrew has the range \u0590-\u05fe (according to this nice JavaScript Unicode Regex generator`.

/^[a-z\u0590-\u05fe]+$/i
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 1
    Should we throw a `$` in there as well for the end of the string? – Casey Falk Jul 31 '14 at 19:38
  • Does it works with "change direction" unicodes? (RLM, LRM, `\u200f` `\u200e`). It could be a problem if the user changes from Hebrew to English and vice versa. My solution covers it https://stackoverflow.com/a/66104183/12695188 – genericUser Oct 14 '21 at 07:35
9

While the selected answer is correct about "Hebrew" the OP wanted to limit validation to only Hebrew and English letters. The Hebrew Unicode adds a lot of punctuation and symbols (as you can see in the table here) irrelevant for such validation. If you want only Hebrew letters (along with English letters) the regex would be:

/^[a-z\u05D0-\u05EA]+$/i

I would consider adding ' (single quote) as well, for foreign consonants that are missing in Hebrew (such as G in George and Ch in Charlie) make use of it along with a letter:

/^[a-z\u05D0-\u05EA']+$/i
Tom Shmaya
  • 91
  • 2
  • 3
7

English & Hebrew FULL regex

I'm using the above regex on my application. My users are just fine with it:

RegExp(r'^[a-zA-Z\u0590-\u05FF\u200f\u200e ]+$');

The regex supports:

  • English letters (includes Capital letters). a-zA-Z
  • Hebrew (includes special end-letters). \u0590-\u05FF
  • change direction unicodes (RLM, LRM). \u200f\u200e
  • White space.

Enjoy!

genericUser
  • 4,417
  • 1
  • 28
  • 73
2

Try this. Not sure if it will work. If not, these references should help.

[A-Za-z\u0590-\u05FF]*

Hebrew Unicode

Unicode in Regular Expressions

1

Hebrew letters only:

/^[\u0590-\u05ea]+$/i
Jonesome Reinstate Monica
  • 6,618
  • 11
  • 65
  • 112
0

You can also use \p{Hebrew} in your regex to detect any Hebrew unicode characters (if you're regex engine supports it).

0

Well, the RegEx pattern is between two /'s. The i at the end is a flag that says to be indifferent to the cases. ^ means the start of a line, and $ means the end of a line. Brackets ([ and ]) means either of the characters inside the brackets. - means a range. Note that the characters are ordinal, so a-z or א-ת make sense; a-z means all letters from and include a to and include z. The same goes for א-ת. + means one or more of the preceding. So this pattern matches every sequence of letters from English or Hebrew.

P.S.: Also, note that the flavor of the RegEx is different in different languages and platforms. for example in Sublime Text the pattern would be: (?i)^[א-תa-z]+$.

/^[א-תa-z]+$/i
Mehdi Abbassi
  • 627
  • 1
  • 7
  • 24
0

Swift 5 This work for me: Hebrew, English

NSRegularExpression(pattern: "^[\\p{Hebrew} a-z]$",  options: .caseInsensitive).firstMatch(in: value, options: [], range: NSRange(location: 0, length: value.count))

Another languages you can get here - https://h2s1880.medium.com/how-to-use-regular-expressions-to-distinguish-national-languages-in-swift-c19d6d8d0a97

Evgeniy
  • 96
  • 1
  • 4