Skip to content

Instantly share code, notes, and snippets.

@ali-master
Last active January 12, 2017 19:55
Show Gist options
  • Save ali-master/76869daaee0aa32db9bffccc9f022e4a to your computer and use it in GitHub Desktop.
Save ali-master/76869daaee0aa32db9bffccc9f022e4a to your computer and use it in GitHub Desktop.
Detect Persian Unicode of Text

Detect Persian Unicode of Text

/**
 * @param {string}
 * @return {boolean}
 */
const isPersian = (str) => {
  const letters = [];
  for (let i = 0; i <= str.length; i++) {
    letters[i] = str.substring((i - 1), i);
    if (letters[i].charCodeAt() > 255) { return true; }
  }
  return false;
}

Usage

const text = "Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator."
const fa = "این یک متن ازمایشی میباشد";

isPersian(text) // return false
isPersian(fa) // return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment