35

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

Mhd.Jarkas
  • 406
  • 2
  • 5
  • 11

6 Answers6

88

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
  • 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
  • 1
    To (approximatively) count the number of arabic characters in the string `string.match(/[\u0600-\u06FF]/g).length` – Rolf Oct 10 '17 at 16:35
9
function isArabic(text) {
    var pattern = /[\u0600-\u06FF\u0750-\u077F]/;
    result = pattern.test(text);
    return result;
}
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
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,660
  • 22
  • 90
  • 120
2
  • 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
}
Ramy Hadid
  • 120
  • 6
1

Ranges for Arabic characters are:

0x600  - 0x6ff

0x750  - 0x77f

0xfb50 - 0xfc3f

0xfe70 - 0xfefc
ZooZ
  • 933
  • 1
  • 17
  • 25
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