22

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

any ideas?

j0k
  • 22,303
  • 28
  • 77
  • 86

5 Answers5

32

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

Joey
  • 330,812
  • 81
  • 665
  • 668
Oded
  • 477,625
  • 97
  • 867
  • 998
  • 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
23

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
  • 31,931
  • 8
  • 74
  • 86
Bart Kiers
  • 161,100
  • 35
  • 287
  • 281
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
  • 928
  • 1
  • 8
  • 26
Oranit Dar
  • 1,281
  • 15
  • 16
  • 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
  • 1,969
  • 23
  • 25
alemjerus
  • 7,623
  • 3
  • 31
  • 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,702
  • 2
  • 32
  • 34