How to set up a regex?

Asked

Viewed 371 times

0

I’m assembling a bot for rocketchat to turn machines on and off in google cloud, I need to assemble a regex that covers the words Turnon and turnoff. Someone could help me with this?

  • 1

    /turnon|turnoff/g?

  • 1

    @vnbrs /turn(on|off)/g?

  • 1

    @Jeffersonquesado /turno(n|ff)/g?

  • 2

    @vnbrs thought about it too, but then lost semantics by a questionable gain of structure

  • 1

    @Jeffersonquesado is when avoid redundancy tends to exaggerate

1 answer

2

As pointed out by @Jeffersonquesado:

/turn(on|off)/g

The first bar indicates the beginning of a regular expression. The parentheses are a capturing group. The pipe acts as a operator ou. Finally, the /g, which is the global search flag.

Behold running on the Regexr.

You can then think about adding the case modifier insensitive, /i, if it helps you.

And there in Coffeescript...

pattern = /^turn(on|off)/g;
"turndownforwhat".match(pattern);
> null
"turnon".match(pattern);
> [ 'turnon' ]

Behold running on repl.it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.