1

I need to test a string (Hebrew) for a specific word. Here is a working fiddle.

It work while you're using an English. So if you test "are you ready" for "are" (true) or "read" (false), it works fine.

But if you test "כל הכבוד" for "כל", it returns false.

JS script I'm using:

var myString = 'Hello world!';
var myWord = 'world';
var regexp = new RegExp('\\b' + myWord + '\\b');
alert(regexp.test(myString));

Does anyone have an idea, why RegExp stops working when you work with Hebrew?

neoselcev
  • 138
  • 12
  • 1
    See http://stackoverflow.com/a/31203405/3832970 or http://stackoverflow.com/a/41608672/3832970. `\b` is not Unicode aware in JS regex. – Wiktor Stribiżew Jan 24 '17 at 09:44
  • 1
    Does `/(^|[^\u0590-\u05FF])כל(?![\u0590-\u05FF])/.test("כל הכבוד")` work for you? Or can there be any other letters other than Hebrew "glued" with Hebrew ones? – Wiktor Stribiżew Jan 24 '17 at 09:56
  • @WiktorStribiżew actually it does!!! Can you please explain how this works? – neoselcev Jan 24 '17 at 10:01
  • 1
    It is described in [my previous answer](http://stackoverflow.com/a/31203405/3832970). The pattern is a copy from the first solution in that answer. – Wiktor Stribiżew Jan 24 '17 at 10:07
  • @WiktorStribiżew Thank you! It works and I do understand the explanation! But this question isn't the exact duplicate (though you do provide the correct answer in it), as the title and the purpose of what is needed are completely different. Can you please move the comment to an answer for this question linked to [you answer](http://stackoverflow.com/questions/31202244/surround-hebrew-and-english-text-in-div/31203405#31203405) and I'll mark it as an answer. And again, Thank you! – neoselcev Jan 24 '17 at 10:25
  • Well, the only difference is that you need to use the regex in a `RegExp#test` method, not `String#replace`. Otherwise, it is the same. I don't think it makes sense to repost the same solution. – Wiktor Stribiżew Jan 24 '17 at 10:27
  • Edit the post to provide a reason why the question must be reopened. If there are reopen votes, I will post the solution. – Wiktor Stribiżew Jan 24 '17 at 10:29
  • @WiktorStribiżew you didn't understand me. Don't repost the solution. Just move the link to the answer from the comment to an answer, as it does answer the question. – neoselcev Jan 24 '17 at 10:37
  • The post is closed, and thus it is not possible to post any more answers. – Wiktor Stribiżew Jan 24 '17 at 10:40
  • @WiktorStriizev Ok. Thanks anyway :) – neoselcev Jan 24 '17 at 10:43

0 Answers0