25

i"m trying to fint if a string starts(first letter) width an RTL language/ hebrew.

any ideas?

j0k
  • 22,600
  • 28
  • 79
  • 90

5 Answers5

35

This will find hebrew letters encoded in the Hebrew Unicode code point range: [\u0590-\u05FF]

Joey
  • 344,408
  • 85
  • 689
  • 683
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • The above is not working for me. Any chance for an example? I'm [trying this](http://jsbin.com/iXOzEHI/1/edit) and it's returning false. – hitautodestruct Jan 16 '14 at 13:20
  • 2
    @hitautodestruct This might be a bit too late, but for reference note that your code sample contains an [en dash](http://www.thepunctuationguide.com/en-dash.html), **–**, instead of a hyphen, **-**. This invalidates the range of your character class and causes the pattern not to match. See [corrected example here](http://jsbin.com/wifememipo/edit). – Boaz Nov 20 '16 at 14:45
  • 2
    @Boaz Thanks for this! Never too late :-) – hitautodestruct Nov 21 '16 at 12:59
25

JavaScript does not support regex scripts like \p{InHebrew} (or something similar). However, it does support Unicode escapes, so you could use a regex like:

/[\u0590-\u05FF]/

which will match a single Hebrew character.

See: http://unicode.org/charts/PDF/U0590.pdf and: http://www.regular-expressions.info/unicode.html

Scimonster
  • 32,893
  • 9
  • 77
  • 89
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
14

    function is_heb(Field) {
        // First choose the required validation

        HebrewChars = new RegExp("^[\u0590-\u05FF]+$");
        AlphaNumericChars = new RegExp("^[a-zA-Z0-9\-]+$");
        EnglishChars = new RegExp("^[a-zA-Z\-]+$");
        LegalChars = new RegExp("^[a-zA-Z\-\u0590-\u05FF ]+$"); //Note that this one allows space 

        // Then use it

        if (!LegalChars.test(Field)) {
            return false;
        } else
            return true;
    }
<input id="the_text" type="text" value="בדיקה" />
<br /><button onclick="document.getElementById('the_result').value = is_heb(document.getElementById('the_text').value)">Is it Hebrew?</button>
<br /><br />
Result:
<br /><input id="the_result" type="text">
LWC
  • 1,084
  • 1
  • 10
  • 28
Oranit Dar
  • 1,539
  • 18
  • 17
  • Hi. This is a great code but I think I messed it up. I'm trying to check for Hebrew characters PLUS space. LegalChars doesn't work for some reason. – user13708028 Dec 23 '21 at 16:26
  • Hi, try this: LegalChars = new RegExp("^[\u0590-\u05FF ]+$"); | https://codverter.com/src/webeditor – Oranit Dar Dec 26 '21 at 05:31
4

if (str.charCodeAt(0) >= 0x590) && (str.charCodeAt(0) <= 0x5FF) then it is considered a hebrew character

jimmont
  • 2,304
  • 1
  • 27
  • 29
alemjerus
  • 8,023
  • 3
  • 32
  • 40
0

Especially for Hebrew the question is answered already - regarding all ranges:

Especially for JS I would recommend a tool to build your regex - see Unicode range RegExp generator (Compiles character ranges suitable for use in JavaScript)

[ just select hebrew or the scripts or ranges you want ]

sebilasse
  • 4,278
  • 2
  • 37
  • 36