javascript 匹配双大括号内的希伯来语单词

标签 javascript regex

我有一个可以用英语工作的 js 函数,但我在希伯来语中遇到错误

该函数匹配 {{ }} 内字符串中的所有单词,并在不带大括号的 array 中返回双大括号内的单词 该字符串可以包含英语和非英语单词,如下所示: {{test}} {{בדקה}} hi this is my test {{test2}}

var str_eng =  "{{test}} {{test1}}";
var str_non_eng = "{{בדיקה}} {{עעע}}";

str_eng.match(/{{\s*[\w\.]+\s*}}/g).map(function(x) { return x.match(/[\w\.]+/)[0];  }) //array[ "test","test1" ] //ok

str_non_eng.match(/{{\s*[\w\.]+\s*}}/g).map(function(x) { return x.match(/[\w\.]+/)[0];  }) // error not working

谢谢!

最佳答案

\w 与希伯来语字符不匹配 - 为此,您需要将希伯来语的 Unicode 范围与 [\u0590-\u05FF] 进行匹配 - 请参阅 here

您可以混合 \w[\u0590-\u05FF] 以匹配 ASCII 和希伯来语:

// var str_non_eng = "{{בדיקה}} {{עעע}}";
var str_non_eng = "{{test}} {{בדיקה}} hi this is my test {{test2}}";

var r1 = /{{\s*[\w\u0590-\u05FF\.]+\s*}}/g
var r2 = /[\w\u0590-\u05FF\.]+/;

var foo = str_non_eng.match(r1).map(function(x) {
  return x.match(r2)[0];
});
console.log(foo);

关于javascript 匹配双大括号内的希伯来语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44997231/

相关文章:

javascript - 如何在此正则表达式中包含 - 和 '?

python - 从包含括号的文本中提取键值对(日志文件)

javascript - 如何将多个参数传递给通过javascript变量绑定(bind)的onclick事件

javascript - Rails3.1 无限/无限滚动

javascript - 更改位置时出现 Safari 渲染问题 :fixed to position:relative

javascript - 返回 promise 后访问工厂服务

javascript - 如何从对象内部的对象获取/寻址元素?

java - 试图确定正确的正则表达式

c# - 从前面带有分隔符的文本中删除单词(使用正则表达式)

python : pass regex back-reference value to method