How to check if any Arabic character exists in the string with javascript language
6 Answers
According to Wikipedia, Arabic characters fall in the Unicode range 0600 - 06FF. So you can use a regular expression to test if the string contains any character in this range:
var arabic = /[\u0600-\u06FF]/;
var string = 'عربية'; // some Arabic string from Wikipedia
alert(arabic.test(string)); // displays true

- 69,683
- 7
- 133
- 150
-
please can i use this with the jQuery, i want to add class to the parent of text if the text is arabic ? – kebir Jun 05 '14 at 14:42
-
1To (approximatively) count the number of arabic characters in the string `string.match(/[\u0600-\u06FF]/g).length` – Rolf Oct 10 '17 at 16:35
function isArabic(text) {
var pattern = /[\u0600-\u06FF\u0750-\u077F]/;
result = pattern.test(text);
return result;
}

- 16,260
- 13
- 59
- 93
how it work for me is
$str = "عربية";
if(preg_match("/^\x{0600}-\x{06FF}]+/u", $str))echo "invalid";
else echo "valid";
You can check extended range of Arabic character
0x600 - 0x6ff
0x750 - 0x77f
0xfb50 - 0xfc3f
0xfe70 - 0xfefc
So expression will look more like "/^\x{0600}-\x{06FF}\x{0750}-\x{077f}]+/u"
Good Luck

- 9,660
- 22
- 90
- 120
- Check if string is arabic:
function isArabic (string) {
let def = 0;
let ar = 0;
string.split('').forEach(i => /[\u0600-\u06FF]/.test(i) ? (ar++) : (def++))
return ar >= def
}

- 120
- 6
Ranges for Arabic characters are:
0x600 - 0x6ff
0x750 - 0x77f
0xfb50 - 0xfc3f
0xfe70 - 0xfefc

- 933
- 1
- 17
- 25
Checkout the npm package I created. https://www.npmjs.com/package/is-arabic It checks both Arabic and Farsi letters and Unicode as well. It also checks for Arabic symbols, Harakat, and numbers. You can also make it check for a certain number of characters.By default it checks if the whole string is Arabic. Use the count option to check if a string includes Arabic characters. It has full support. Check it out.
Example:
const isArabic = require("is-arabic");
const text = "سلام";
// Checks if the whole string is Arabic
if (isArabic(text)){
// Do something
}
// Check if string includes Arabic characters
// count: The number of Arabic characters occurrences for the string to be considered Arabic
const text2 = "مرحبا Hello";
const options = { count: 4 };
const includesArabic = isArabic(text, options);
console.log(includesArabic); // true

- 59
- 4