Skip to content

Instantly share code, notes, and snippets.

@Rouhollah
Last active March 29, 2023 09:15
Show Gist options
  • Save Rouhollah/1d4de6289628aba416a494dd7c1cbd7a to your computer and use it in GitHub Desktop.
Save Rouhollah/1d4de6289628aba416a494dd7c1cbd7a to your computer and use it in GitHub Desktop.
recognition of Persians text
You can use the following regular expression to accept only Persian letters:
/^[\u0600-\u06FF\s]+$/
This regular expression accepts Persian letters and spaces. If you want to exclude spaces, you can use:
/^[\u0600-\u06FF]+$/
Here are some examples of how you can use this regular expression in JavaScript:
var persianText = "سلام دنیا";
var englishText = "Hello World";
var persianRegex = /^[\u0600-\u06FF\s]+$/;
var englishRegex = /^[a-zA-Z\s]+$/;
console.log(persianRegex.test(persianText)); // true
console.log(englishRegex.test(englishText)); // true
console.log(persianRegex.test(englishText)); // false
console.log(englishRegex.test(persianText)); // false
(1): [How to set direction (ltr / rtl) according to its inner text using JavaScript?](https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393)
Source: Conversation with Bing, 3/29/2023(1) java - Regular expression for persian(arabic) letters without any .... https://stackoverflow.com/questions/30115992/regular-expression-for-persianarabic-letters-without-any-numbers Accessed 3/29/2023.
(2) Dedicated Regular Expression for Persian alphabet. https://stackoverflow.com/questions/17315989/dedicated-regular-expression-for-persian-alphabet Accessed 3/29/2023.
(3) c# - regex for accepting only persian characters - Stack Overflow. https://stackoverflow.com/questions/22565100/regex-for-accepting-only-persian-characters Accessed 3/29/2023.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment