Skip to content
Homayoun edited this page Jul 9, 2026 · 1 revision

Introduced in v0.2.0

⚠️ Version Recommendation: Always install and use the latest version of rtl-text-tools (npm install rtl-text-tools@latest). While this specific feature was introduced in an earlier version, the latest release includes crucial bug fixes, better edge-case handling, and improved legacy browser compatibility. Avoid pinning your project to an old version just to use this function.

Detects if a given string contains any Right-to-Left (RTL) characters. This is the primary way to check if text needs RTL processing before applying other transformations.

πŸ“ Syntax

hasRTL(text: string): boolean

Parameters

Name Type Description
text string The text to check for RTL characters.

Returns

  • boolean : Returns true if the text contains at least one RTL character, otherwise false. Returns false for empty strings, null, or undefined.

πŸ’‘ Examples

Basic Usage

import { hasRTL } from 'rtl-text-tools';

hasRTL("Hello");   // false (English/LTR)
hasRTL("Ω…Ψ±Ψ­Ψ¨Ψ§");   // true  (Arabic/Persian)
hasRTL("Χ©ΧœΧ•Χ");    // true  (Hebrew)
hasRTL("123");     // false (Numbers only)
hasRTL("");        // false (Empty string)

Conditional Processing

Use it to conditionally apply RTL fixes only when necessary, saving processing time for purely LTR content:

import { hasRTL, fixRTL } from 'rtl-text-tools';

const userInput = "Hello world!";

if (hasRTL(userInput)) {
  console.log(fixRTL(userInput)); // Process only if RTL is detected
} else {
  console.log("No RTL processing needed.");
}

🌍 Supported RTL Scripts

This function checks against a comprehensive set of Unicode ranges covering all major RTL scripts:

Script Unicode Range
Hebrew \u0590-\u05FF
Arabic (Core, includes Persian/Urdu) \u0600-\u06FF
Syriac \u0700-\u074F
Arabic Supplement \u0750-\u077F
Thaana (Maldivian) \u0780-\u07BF
N'Ko \u07C0-\u07FF
Samaritan \u0800-\u083F
Mandaic \u0840-\u085F
Arabic Extended-A \u08A0-\u08FF
Hebrew Presentation Forms \uFB1D-\uFB4F
Arabic Presentation Forms-A \uFB50-\uFDFF
Arabic Presentation Forms-B \uFE70-\uFEFF

βš™οΈ Technical Details & Compatibility

  • IE11 Compatibility: The underlying regular expression intentionally avoids the ES6 u (unicode) flag to ensure full compatibility with legacy browsers like IE11.
  • BMP Only: It relies entirely on Basic Multilingual Plane (BMP) code points (\uXXXX), which work perfectly without the u flag.
  • Performance: The regex is optimized for fast execution, returning true the moment it finds the first matching character without scanning the entire string.

πŸ“œ Version History

  • v0.2.0: Renamed from containsRTL() to hasRTL() for better naming conventions.
  • v0.1.0: Feature originally introduced as containsRTL() in the initial release.

πŸš€ rtl-text-tools

npm downloads

πŸ‘¨β€πŸ’» Created by

Homayoun Mohammadi
πŸ”— GitHub β€’ βœ‰οΈ Email

⭐ Love this project? Star it on GitHub to show your support!

Clone this wiki locally