javascript+动态删除阿拉伯语文本变音符号 [英] javascript+remove arabic text diacritic dynamically

查看:18
本文介绍了javascript+动态删除阿拉伯语文本变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态删除阿拉伯语变音符号我正在设计一本电子书chm"并且有多个 html 页面包含阿拉伯文本但有时搜索引擎想要突出显示一些阿拉伯语单词因为它的变音符号所以在页面加载时有可能使用 JavaScript 函数来去除阿拉伯语变音符号文本吗?但必须有再次启用的选项,所以我不想从 HTML 物理上删除它,而是暂时的,

how to remove dynamically Arabic diacritic I'm designing an ebook "chm" and have multi html pages contain Arabic text but some time the search engine want highlight some of Arabic words because its diacritic so is it possible when page load to use JavaScript functions that would strip the Arabic diacritic text ?? but must have option to enabled again so i don't want to remove it from HTML physically but temporary,

问题是我不知道从哪里开始以及使用什么是正确的功能

the thing is i don't know where to start and what is the right function to use

谢谢:)

例如

Text : الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ
converted to : الحمد لله رب العالمين 

推荐答案

我编写了这个函数来处理混合阿拉伯语和英语字符的字符串,删除特殊字符(包括变音符号)和规范化一些阿拉伯字符,比如将所有的 ة 转换为 ه .

I wrote this function which handles strings with mixed Arabic and English characters, removing special characters (including diacritics) and normalizing some Arabic characters like converting all ة's into ه's.

normalize_text = function(text) {

  //remove special characters
  text = text.replace(/([^\u0621-\u063A\u0641-\u064A\u0660-\u0669a-zA-Z 0-9])/g, '');

  //normalize Arabic
  text = text.replace(/(آ|إ|أ)/g, 'ا');
  text = text.replace(/(ة)/g, 'ه');
  text = text.replace(/(ئ|ؤ)/g, 'ء')
  text = text.replace(/(ى)/g, 'ي');

  //convert arabic numerals to english counterparts.
  var starter = 0x660;
  for (var i = 0; i < 10; i++) {
    text.replace(String.fromCharCode(starter + i), String.fromCharCode(48 + i));
  }

  return text;
}

<input value="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" type="text" id="input">
<button onclick="document.getElementById('input').value = normalize_text(document.getElementById('input').value)">Normalize</button>

这篇关于javascript+动态删除阿拉伯语文本变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆