21

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

any ideas?

j0k
  • 21,914
  • 28
  • 75
  • 84

5 Answers5

30

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

Joey
  • 316,376
  • 76
  • 642
  • 652
Oded
  • 463,167
  • 92
  • 837
  • 979
  • 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
21

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
  • 30,695
  • 8
  • 67
  • 84
Bart Kiers
  • 153,868
  • 34
  • 276
  • 272
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
  • 804
  • 8
  • 26
Oranit Dar
  • 1,023
  • 12
  • 13
4

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

jimmont
  • 1,699
  • 21
  • 24
alemjerus
  • 7,131
  • 2
  • 30
  • 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
  • 3,202
  • 2
  • 30
  • 30