Javascript - how to find hebrew?
i"m trying to fint if a string starts(first letter) width an RTL language/ hebrew.开发者_运维百科
any ideas?
This will find hebrew letters encoded in the Hebrew Unicode code point range: [\u0590-\u05FF]
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
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">
if (str.charCodeAt(0) >= 0x590) && (str.charCodeAt(0) <= 0x5FF) then
it is considered a hebrew character
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 ]
精彩评论