How to do "regex.test(string)" in Macrodroid? - Solved

AkashiDom

Active member
JavaScript:
const username = "myBot";
const regex = new RegExp(`^\\/\\w+(\\@${username})?(\\s|$)`);
const string = "/ping@otherbot ms";

if (regex.test(string)) {
    console.log(true);
} else {
    console.log(false);
};
Is there anyway to replicate that JavaScript code to macrodroid?
 
Last edited:

AkashiDom

Active member
5 hours later and no help
If you don't understand, I just want to know how to do like regularExpression.test(string) in macrdroid
 

Endercraft

Moderator (& bug finder :D)
You can use the text manipulation (extract text) action and see if the result is empty or you can use a constraint with compare values (string) and enable regex matching (recommended). You can also use the JavaScript action to run your code ([lv=varname] is evaluated in the code).
 

AkashiDom

Active member
You can use the text manipulation (extract text) action and see if the result is empty or you can use a constraint with compare values (string) and enable regex matching (recommended). You can also use the JavaScript action to run your code ([lv=varname] is evaluated in the code).
I want to do the 2nd but I don't quite understand
Do I have to put / at front and end of my regular expression or regex?
 

Yogi

Member
So let's say I make regex of checking if it a word, all I need to input is \w+ and not /\w+/?
I'm not a profession with regex and javascript and mostly do with try and error.
Best is to simplify the regex and then go on. For your word example it should be
JavaScript:
"\\w+"
so the backslash as a special char has to be escaped. I took your example code, reduced the regex to
JavaScript:
"\\w+"+username
and changed the string to
Code:
"test myBot"
it worked with console output
JavaScript:
true

hope it helps,
regards
Fritz
 

AkashiDom

Active member
Thanks everyone, I finally understand how to put regular expression in compare value or macrodroid variable
 

LinerSeven

Active member
Thanks everyone, I finally understand how to put regular expression in compare value or macrodroid variable
Hi, @AkashiDom ,

It may be too late now.....

Regex was getting awfully difficult, so I tried to interpret it simply. I attach a sample macro.

The result of the macro should be True, right?
The Regex part seemed to be the problem, not the structure of your source code.

Best Regards,
Liner Seven,
 

Attachments

  • Screenshot_20240206-181759.png
    Screenshot_20240206-181759.png
    151.2 KB · Views: 16
  • RegexTest20240206.macro
    1.9 KB · Views: 6
Top