Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

Simple js function to detect Arabic from English script.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-3.3-70b-instruct’s answer.
  1. User

    Simple js function to detect Arabic from English script.

  2. Assistant

    Certainly! Here's a simple JavaScript function that detects if a given string contains Arabic characters. Arabic script characters are generally in the Unicode range \u0600 to \u06FF (Arabic block), and also some extended Arabic characters in other ranges.

    This function checks if the input string contains any Arabic character:

    function containsArabic(text) {
      // Arabic Unicode block ranges:
      // \u0600-\u06FF (Arabic)
      // \u0750-\u077F (Arabic Supplement)
      // \u08A0-\u08FF (Arabic Extended-A)
      // \uFB50-\uFDFF (Arabic Presentation Forms-A)
      // \uFE70-\uFEFF (Arabic Presentation Forms-B)
      const arabicRegex = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/;
      return arabicRegex.test(text);
    }
    
    // Example usage:
    console.log(containsArabic("Hello"));       // false
    console.log(containsArabic("مرحبا"));       // true
    console.log(containsArabic("Hello مرحبا")); // true
    

    Explanation:

    • The regex looks for any character in the Arabic Unicode blocks.
    • If any Arabic character is found, it returns true.
    • Otherwise, it returns false.

    Let me know if you want a version that detects only English letters or distinguishes between Arabic and English more strictly!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.