4
\$\begingroup\$

In my answer, I have this code:

s.match(/[AEIOU]/)

This seems too verbose to check if a string is one of A, E, I, O, or U.

I tried:

"AEIOU".includes(s)

But that's 1 byte longer. Can I do this in a shorter way?

\$\endgroup\$
4
  • 5
    \$\begingroup\$ Before anyone close-votes this, this is a valid tips question. \$\endgroup\$
    – hyper-neutrino
    Commented Jul 20, 2017 at 21:08
  • \$\begingroup\$ I don't think so ... \$\endgroup\$
    – Adalynn
    Commented Jul 20, 2017 at 21:09
  • 1
    \$\begingroup\$ ~"AEIOU".indexOf(s) also works. \$\endgroup\$
    – Stephen
    Commented Jul 20, 2017 at 21:14
  • \$\begingroup\$ @CloseVoter(s) how is this too broad? It's asking for golfing advice for a specific snippet. \$\endgroup\$
    – user58826
    Commented Jul 20, 2017 at 21:41

3 Answers 3

6
\$\begingroup\$

Try /[AEIOU]/.test(s)

Body must be at least 30 characters; you entered 21

\$\endgroup\$
1
  • 1
    \$\begingroup\$ That's a great way around the character minimum. I almost didn't notice it. \$\endgroup\$ Commented Aug 22, 2017 at 17:22
4
\$\begingroup\$

If s is a literal and not a variable, you can do the following:

/[AEIOU]/.test`A`
"AEIOU".includes`A`
~"AEIOU".indexOf`A`

which is shorter than

/[AEIOU]/.test("A")
"AEIOU".includes("A")
~"AEIOU".indexOf("A")
\$\endgroup\$
3
  • \$\begingroup\$ Though if it's one of the former the result is deterministic and you can golf it down to one byte 1 and 0 \$\endgroup\$
    – Downgoat
    Commented Jul 21, 2017 at 4:23
  • \$\begingroup\$ @Downgoat I was mostly sharing the backticks trick - usually this would be used with "AEIOU" is not a constant, so s.includes<backtick>A<backtick> \$\endgroup\$
    – Stephen
    Commented Jul 21, 2017 at 11:30
  • \$\begingroup\$ The backticks trick isn't relevant here... \$\endgroup\$
    – ASCII-only
    Commented Apr 25, 2018 at 7:08
2
\$\begingroup\$

s.match`[AEIOU]` goes well.....

\$\endgroup\$
3
  • \$\begingroup\$ That doesn't work... \$\endgroup\$
    – ASCII-only
    Commented Apr 25, 2018 at 7:10
  • 1
    \$\begingroup\$ @ASCII-only, it works, just l4m2 had to pay the price of not using code block markup. \$\endgroup\$
    – manatwork
    Commented Apr 25, 2018 at 8:08
  • \$\begingroup\$ @manatwork ah, I see \$\endgroup\$
    – ASCII-only
    Commented Apr 25, 2018 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.