2

i have a js function that working in english but i get error in hebrew

the function match all the words in string that inside {{ }} and return the word inside the double curly bracket in a array without the curly bracket the string can Contains both english and non english words like this: {{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

Thanks!

Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
shlomicohen
  • 101
  • 1
  • 7
  • Replace `[\w\.]+` with `.*?` in the first bit, and just remove the brackets via `substr()` in your `map`. – Niet the Dark Absol Jul 09 '17 at 14:08
  • Possible duplicate of [Regex to get string between curly braces "{I want what's between the curly braces}"](https://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace) – str Jul 09 '17 at 14:09
  • @NiettheDarkAbsol can you show me how? thanks – shlomicohen Jul 09 '17 at 14:15

2 Answers2

2

\w does not match Hebrew characters - to do that you need to match on the Unicode range for Hebrew with [\u0590-\u05FF] - see here.

You can blend \w and [\u0590-\u05FF] to match both ASCII and Hebrew:

// 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);
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
0

you can't use \w for Hebrew chars. use the following :

/{{\s*[\w\u0590-\u05FF\.]+\s*}}/g
Niv Apo
  • 983
  • 1
  • 9
  • 18