28

How to check if any Arabic character exists in the string with javascript language

Mhd.Jarkas
  • 336
  • 2
  • 4
  • 11

5 Answers5

76

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
casablanca
  • 68,094
  • 7
  • 131
  • 148
  • 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
  • To (approximatively) count the number of arabic characters in the string `string.match(/[\u0600-\u06FF]/g).length` – Rolf Oct 10 '17 at 16:35
7
function isArabic(text) {
    var pattern = /[\u0600-\u06FF\u0750-\u077F]/;
    result = pattern.test(text);
    return result;
}
Furqan Safdar
  • 15,658
  • 12
  • 57
  • 89
3

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

Wasim A.
  • 9,201
  • 21
  • 89
  • 117
1

Ranges for Arabic characters are:

0x600  - 0x6ff

0x750  - 0x77f

0xfb50 - 0xfc3f

0xfe70 - 0xfefc
ZooZ
  • 835
  • 1
  • 17
  • 23
0

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