Does the Given String Contain Hebrew

Hello everyone,
I have a macro that I want to run on the condition the string in a named clipboard contains Hebrew letters.
I searched the web and couldn't find how to do this. I assume I need regex, but I don't have the skills for that.
Any ideas?

You do need regex for this, but fortunately, it's very simple. All you need to do is set your condition to use "matches" instead of "contains," and use \p{Hebrew} for the regex. It should look something like this, only with your named clipboard instead of text:

4 Likes

Thank you @gglick so much.
works beautifully. much appreciated.

1 Like

Thanks – I didn't know there was a named character class – I thought we had to literally search for characters drawn from the Unicode code block [\u0590-\u05FF]

1 Like

That works too, of course, but yes, there are named classes for Hebrew, as well as many other languages and character sets. Peter actually hosts a comprehensive list of them at https://unicode.keyboardmaestro.com/ and links to it in KM's Help menu, though many if not all include further qualifiers like Script= that I haven't found necessary in practice, at least when using them in KM.

1 Like

FWIW I notice that character class names are not recognised quite so flexibly by the JS regex engine, so within an Execute JavaScript for Automation action we would need a fully specified name like \p{Script=Hebrew}:

(() => {
    "use strict";

    return [
            'A new year – תשפ"ב – has begun',
            'an old year – 2021 – comes to the last quarter'
        ]
        .map(s => (/\p{Script=Hebrew}/u).test(s));
})();

(or, of course, the addresses of the Unicode block)

Expand disclosure triangle to view variant JS Source
(() => {
    "use strict";

    return [
            'A new year – תשפ"ב – has begun',
            'an old year – 2021 – comes to the last quarter'
        ]
        .map(s => (/[\u0590-\u05FF]/u).test(s));
})();
2 Likes