在div中环绕希伯来文和英文文本 [英] Surround Hebrew and English text in div

查看:97
本文介绍了在div中环绕希伯来文和英文文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在段落中添加希伯来语和英语句子的span标签。例如。
这样所有的东西都变成了什么?
会变成:

$ $ $ $ $ $ $ $ $ $ [/ span] [/ span] [/ span]

我一直在尝试使用正则表达式但它只是删除希伯来文的单词,并在一个范围内加入英文单词。

  var str ='so all whats upאתכם?'
var match = str.match(/(\\ \\b [AZ] + \b)/ IG);
var replace = match.join()。replace(match.join(),'< span>'+ match.join()+'< / span>')


解决方案

以前的答案并未解释整个词的需求。事实上,很难实现这一点,因为 \ b 字边界不支持与邻居希伯来Unicode符号的单词边界,我们只能使用<$ c匹配字符类$ c> \u 表示法。



我建议使用前瞻和捕捉组来确保我们捕捉到整个希伯来字((^ | [^ \\\֐-\\ \\ u05FF])([\\\֐-\\\׿] +)(?![\\\֐-\\\׿])确保有一个非希伯来符号或字符串的开始在希伯来词之前 - 如果希伯来词之间有空格,则添加 \ s )和 \ b [az \ s] + \ b 来匹配以空格分隔的整个英文单词的序列。



如果您计划插入< ; span> 标记到整个单词的句子中,这里有一个函数可以帮助:
$ b

< 'so so all whats upאתכם?'; // var str ='so,היי,all whats upאתכם?'; var result = str.replace(/ \s *(\ b [az\s] + \b)\s * / ig,'< span> $ 1< / span&g '); result = result.replace(/(^ | [^ \\\֐-\\\׿])([\\\֐-\\\׿] +)(?![\\\֐-\\\׿] )/ g,'$ 1< span> $ 2< / span>'); document.getElementById(r)。innerHTML = result;

  span {background:#FFCCCC; border:1px solid#0000FF;}  

< div width =645id =r/>



 < span> so< / span>< span>היי< / span>< span> ; /跨度><跨度>אתכם< /跨度> ;? 

如果您的输出中不需要任何标点符号或字母数字实体,只需连接整个英文和希伯来词,然后使用

var str ='היי,User234,so 222הייall whats upאתכם?'; var re = /(^ | [^ \\\֐-\\\׿])([\\\֐-\\\׿] +)([\\\֐-\\\׿])|?!(\b [az\s] + \b) / ig; var res = []; while((m = re.exec(str))!== null){if(m.index === re.lastIndex){re.lastIndex ++; } if(m [1]!== undefined){res.push('< span>'+ m [2] .trim()+'< / span>'); } else {res.push('< span>'+ m [3] .trim()+'< / span>'); }} document.getElementById(r)。innerHTML = res.join();

  span {background:#FFCCCC; border:1px solid#0000FF;}  

< div width =645id =r/>



 < span>היי< / span>< span> so< / span>< span>היי< span>< span>所有最新消息< / span>< span>个人资料< / span> 


I am trying to add a span tag around Hebrew and English sentence in a paragraph. E.g. "so היי all whats up אתכם?" will become :

[span]so[/span][span]היי[/span][span]all whats up[/span][span]אתכם[/span]

I have been trying with regexp but its just removing the Hebrew words and joining the English words in one span.

var str = 'so היי all whats up אתכם?'
var match= str.match(/(\b[a-z]+\b)/ig);
var replace = match.join().replace(match.join(),'<span>'+match.join()+'</span>')

解决方案

Previous answers here did not account for the whole word requirement. Indeed, it is difficult to achieve this since \b word boundary does not support word boundaries with neighboring Hebrew Unicode symbols that we can only match with a character class using \u notation.

I suggest using look-aheads and capturing groups to make sure we capture the whole Hebrew word ((^|[^\u0590-\u05FF])([\u0590-\u05FF]+)(?![\u0590-\u05FF]) that makes sure there is a non-Hebrew symbol or start of string before a Hebrew word - add a \s if there are spaces between the Hebrew words!), and \b[a-z\s]+\b to match sequence of whole English words separated with spaces.

If you plan to insert the <span> tags into a sentence around whole words, here is a function that may help:

var str = 'so היי all whats up אתכם?';
//var str = 'so, היי, all whats up אתכם?';
var result = str.replace(/\s*(\b[a-z\s]+\b)\s*/ig, '<span>$1</span>');
result = result.replace(/(^|[^\u0590-\u05FF])([\u0590-\u05FF]+)(?![\u0590-\u05FF])/g, '$1<span>$2</span>');
document.getElementById("r").innerHTML = result;

span {
    background:#FFCCCC;
    border:1px solid #0000FF;
}

<div width="645" id="r"/>

Result:

<span>so</span><span>היי</span><span>all whats up</span><span>אתכם</span>?

If you do not need any punctuation or alphanumeric entities in your output, just concatenated whole English and Hebrew words, then use

var str = 'היי, User234, so 222היי all whats up אתכם?';
var re = /(^|[^\u0590-\u05FF])([\u0590-\u05FF]+)(?![\u0590-\u05FF])|(\b[a-z\s]+\b)/ig;
var res = [];
while ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
  if (m[1] !== undefined) {
      res.push('<span>'+m[2].trim()+'</span>');
    }
  else
    {
      res.push('<span>'+m[3].trim()+'</span>');
    }
  
}
document.getElementById("r").innerHTML = res.join("");

span {
    background:#FFCCCC;
    border:1px solid #0000FF;
}

<div width="645" id="r"/>

Result:

<span>היי</span><span>so</span><span>היי</span><span>all whats up</span><span>אתכם</span>

这篇关于在div中环绕希伯来文和英文文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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