-
Notifications
You must be signed in to change notification settings - Fork 0
removePersianDiacritics
β οΈ Version Recommendation: Always install and use the latest version ofrtl-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.
Removes Arabic diacritics (Harakat / E'rab) from the text.
Persian rarely uses vowel marks (Fatha, Kasra, Damma, Sukun, etc.) in standard writing. This function strips them out to leave the clean, base text, which is highly useful for search indexing, text comparison, and normalizing user input.
β οΈ Important Warning: Do not use this function if you are processing Quranic texts, classical poetry, or children's books where vowel marks are strictly required for correct pronunciation and meaning.
removePersianDiacritics(text: string): string| Name | Type | Description |
|---|---|---|
text |
string |
The text containing Arabic diacritics to remove. |
-
string: Returns the text with all diacritics in the\u064Bto\u065Frange stripped out. Returns the original string unchanged if it is empty,null, or contains no diacritics.
import { removePersianDiacritics } from 'rtl-text-tools';
removePersianDiacritics("Ω
ΩΨΩΩ
ΩΩΨ―"); // "Ω
ΨΩ
Ψ―"
removePersianDiacritics("Ψ§ΩΩΩΨΩΩ
ΩΨ―Ω ΩΩΩΩΩΩΩ"); // "Ψ§ΩΨΩ
Ψ― ΩΩΩ"
removePersianDiacritics("Ψ΄ΩΨ¬ΩΨ±ΩΨ©Ω"); // "Ψ΄Ψ¬Ψ±Ψ©" (Teh Marbuta is kept, only diacritics removed)
removePersianDiacritics("Ψ¨Ψ―ΩΩ Ψ§ΨΉΨ±Ψ§Ψ¨"); // "Ψ¨Ψ―ΩΩ Ψ§ΨΉΨ±Ψ§Ψ¨" (No diacritics = unchanged)When building a search feature for Persian or Arabic, users will almost never type diacritics. Stripping diacritics from your database content ensures that searches match regardless of how the text was originally vocalized:
import { removePersianDiacritics } from 'rtl-text-tools';
const databaseText = "ΩΩΨͺΩΨ§Ψ¨Ω Ω
ΩΩΩΩΨ―Ω"; // Vocalized text from a database
const userQuery = "Ϊ©ΨͺΨ§Ψ¨ Ω
ΩΫΨ―"; // User typed without diacritics
const normalizedDB = removePersianDiacritics(databaseText); // "ΩΨͺΨ§Ψ¨ Ω
ΩΩΨ―"
const normalizedQuery = removePersianDiacritics(userQuery); // "Ϊ©ΨͺΨ§Ψ¨ Ω
ΩΫΨ―"
// Note: You might also want to combine this with normalizePersianChars()
// to handle the Yeh/Kaf differences before comparing!This function targets the Unicode block from \u064B to \u065F, which includes the most common Arabic vowel marks and signs:
| Character | Name | Unicode |
|---|---|---|
| Ω | Fathatan | \u064B |
| Ω | Dammatan | \u064C |
| Ω | Kasratan | \u064D |
| Ω | Fatha | \u064E |
| Ω | Damma | \u064F |
| Ω | Kasra | \u0650 |
| Ω | Shadda | \u0651 |
| Ω | Sukun | \u0652 |
| (and others) | Small high ligatures, Maddah, etc. |
\u0653 - \u065F
|
-
Regex Character Class: The function uses a highly efficient regular expression (
/[\u064B-\u065F]/g) to match the entire range of diacritics in a single pass, replacing them with an empty string''. - Base Characters Preserved: It strictly targets the combining marks. Base letters (like Alef, Ba, Teh Marbuta, etc.) are completely untouched.
-
IE11 Compatibility: It avoids modern string methods and the ES6
uregex flag, ensuring full compatibility with legacy browsers like IE11. -
Safe Execution: If the input is falsy (e.g.,
null,undefined, or""), it immediately returns the input without throwing an error.
-
v1.2.0: Added
removePersianDiacritics()as part of the Persian language enhancements to clean up text for search and normalization.
π Home β’ π Getting Started β’ π API Reference β’ π Report Issue
Made with β€οΈ by Homayoun Mohammadi
If you find this project useful, please consider β starring the repo to show your support!
- π Home
- π Getting Started
- π§© API Overview
- π οΈ Framework Guides
- π‘ Use Cases
- π₯οΈ Browser Compatibility
- β FAQ & Troubleshooting
Homayoun Mohammadi
π GitHub β’ βοΈ Email
β Love this project? Star it on GitHub to show your support!