我有一个可以用英语工作的 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/