Mhd.Jarkas
Mhd.Jarkas

Reputation: 414

How to check if any Arabic character exists in the string ( javascript )

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

Upvotes: 36

Views: 35985

Answers (6)

Ramy Hadid
Ramy Hadid

Reputation: 162

  • 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
}

Upvotes: 2

Abdulla Saeed
Abdulla Saeed

Reputation: 69

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

Upvotes: 0

Wasim A.
Wasim A.

Reputation: 9866

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

Upvotes: 3

Furqan Safdar
Furqan Safdar

Reputation: 16728

function isArabic(text) {
    var pattern = /[\u0600-\u06FF\u0750-\u077F]/;
    result = pattern.test(text);
    return result;
}

Upvotes: 9

ZooZ
ZooZ

Reputation: 981

Ranges for Arabic characters are:

0x600  - 0x6ff

0x750  - 0x77f

0xfb50 - 0xfc3f

0xfe70 - 0xfefc

Upvotes: 1

casablanca
casablanca

Reputation: 70721

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

Upvotes: 91

Related Questions