2

I have been wondering, how to use case insensitive regular expression for a response validation in a form where there are different possible answers?

I have tried:
(name1|name2|name3)
and it really works, but I want it to be case insensitive.

Is there an easiest way to make it Case Insensitive?

I did:

  1. [?i(name1|name2|name3)] – it didn't work, and it accepts names that are not in the parenthesis.
  2. [?i](name1|name2|name3) – same as what happened to number 1

Any help would be very much appreciated!

2 Answers 2

3

Use a non-capturing group to set flags, like this:

(?i)(name1|name2|name3)

See RE2.

1
-1

Try using this

[?i]name1|name2|name3|

It worked on mine.

2
  • Thanks @Kin ^_^
    – JDD
    Commented Sep 13, 2022 at 4:05
  • That regex will match iname1, ?name1, name2, name3 and also an empty text string, but it will not match name1, Name1, Name2 or nAME3 as specified in the question. Square brackets [] signify a character class rather than a group. Commented Jun 23, 2023 at 14:24

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.